<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 易而优团队 by 小虎哥 <1105415366@qq.com>
 * Date: 2018-4-3
 */

namespace app\admin\controller;

use think\Db;
use think\Page;
use app\common\logic\ArctypeLogic;
use app\admin\logic\ProductLogic;
use app\admin\logic\ProductSpecLogic; // 用于商品规格逻辑功能处理

class ShopProduct extends Base
{
    // 模型标识
    public $nid = 'product';
    // 模型ID
    public $channeltype = 2;
    // 表单类型
    public $attrInputTypeArr = array();

    public function _initialize()
    {
        parent::_initialize();

        $channeltype_list  = config('global.channeltype_list');
        $this->channeltype = $channeltype_list[$this->nid];
        empty($this->channeltype) && $this->channeltype = 2;
        $this->assign('nid', $this->nid);
        $this->assign('channeltype', $this->channeltype);
        // 分类业务层
        $this->arctypeLogic = new ArctypeLogic();
        // 规格业务层
        $this->productSpecLogic = new ProductSpecLogic;
        // 商品模型层
        $this->productModel = model('Product');

        $this->attrInputTypeArr = config('global.attr_input_type_arr');
        // 商城产品参数表
        $this->shop_product_attrlist_db = Db::name('shop_product_attrlist');
    }

    // 商品列表
    public function index()
    {
        // 默认查询条件
        $condition = [
            'a.is_del' => 0,
            'a.merchant_id' => 0,
            'a.channel' => $this->channeltype,
        ];

        // 获取到所有GET参数
        $param = input('param.');

        // 搜索、筛选查询条件处理
        $stypeidwhere = '';
        foreach (['title', 'stypeid'] as $key) {
            if (!empty($param[$key])) {
                if ($key == 'title') {
                    $condition['a.title'] = array('LIKE', "%{$param[$key]}%");
                } else if ($key == 'stypeid') {
                    $this->assign('stypeidStr', implode(',', $param[$key]));
                    $this->assign('stypeidCount', '已选中 ' . count($param[$key]) . ' 个分类');
                    $stypeid = [];
                    foreach ($param[$key] as $value_1) {
                        $stypeid[] = '(a.stypeid LIKE \'%,' . $value_1 .',%\')';
                    }
                    $stypeidwhere = !empty($stypeid) ? implode(' OR ', $stypeid) : '';
                }
            }
        }

        // 权限控制 by 小虎哥
        $admin_info = session('admin_info');
        if (0 < intval($admin_info['role_id'])) {
            $auth_role_info = $admin_info['auth_role_info'];
            if (!empty($auth_role_info) && isset($auth_role_info['only_oneself']) && 1 == $auth_role_info['only_oneself']) {
                $condition['a.admin_id'] = $admin_info['admin_id'];
            }
        }

        // 发布时间查询
        if (!empty($param['add_time'])) {
            $add_time = explode('~', $param['add_time']);
            $start = strtotime(rtrim($add_time[0]));
            $finish = strtotime(rtrim($add_time[1]).' 23:59:59');
            $condition['a.add_time'] = ['between', "$start, $finish"];
        }

        // 自定义排序
        $orderby = input('param.orderby/s');
        $orderway = input('param.orderway/s');
        if (!empty($orderby) && !empty($orderway)) {
            $orderby = "a.{$orderby} {$orderway}, a.aid desc";
        } else {
            $orderby = "a.aid desc";
        }

        // 商品标签查询
        $param['label_types_id'] = !empty($param['label_types_id']) ? strval($param['label_types_id']) : '';
        if (!empty($param['label_types_id'])) {
            // 查询选中指定商品标签的商品ID
            $labelAids = Db::name('product_label')->where('types_id', $param['label_types_id'])->column('aid');
            $condition['a.aid'] = ['IN', $labelAids];
        }

        // 状态查询
        $warning = [];
        $param['query'] = !empty($param['query']) ? intval($param['query']) : 1;
        if ((!empty($param['query']) && 1 === intval($param['query']))) {
            $condition['a.status'] = 1;
            $condition['a.stock_count'] = ['gt', 0];
        } else if (!empty($param['query']) && 2 === intval($param['query'])) {
            $condition['a.status'] = 0;
        } else if (!empty($param['query']) && 3 === intval($param['query'])) {
            $warning['a.stock_count|b.spec_stock'] = ['elt', $this->usersConfig['goods_stock_warning']];
        } else if (!empty($param['query']) && 4 === intval($param['query'])) {
            $condition['a.is_del'] = 1;
        }

        // 销量查询
        $param['sales_num1'] = !empty($param['sales_num1']) ? intval($param['sales_num1']) : 0;
        $param['sales_num2'] = !empty($param['sales_num2']) ? intval($param['sales_num2']) : 0;
        if (!empty($param['sales_num1']) && empty($param['sales_num2'])) {
            $condition['a.sales_num'] = ['gt', $param['sales_num1']];
        } else if (empty($param['sales_num1']) && !empty($param['sales_num2'])) {
            $condition['a.sales_num'] = ['lt', $param['sales_num2']];
        } else if (!empty($param['sales_num1']) && !empty($param['sales_num2'])) {
            $sales_num1 = intval($param['sales_num1']);
            $sales_num2 = intval($param['sales_num2']);
            $condition['a.sales_num'] = ['between', "$sales_num1, $sales_num2"];
        }

        // 价格查询
        $param['users_price1'] = !empty($param['users_price1']) ? intval($param['users_price1']) : 0;
        $param['users_price2'] = !empty($param['users_price2']) ? intval($param['users_price2']) : 0;
        if (!empty($param['users_price1']) && empty($param['users_price2'])) {
            $condition['a.users_price'] = ['gt', $param['users_price1']];
        } else if (empty($param['users_price1']) && !empty($param['users_price2'])) {
            $condition['a.users_price'] = ['lt', $param['users_price2']];
        } else if (!empty($param['users_price1']) && !empty($param['users_price2'])) {
            $users_price1 = intval($param['users_price1']);
            $users_price2 = intval($param['users_price2']);
            $condition['a.users_price'] = ['between', "$users_price1, $users_price2"];
        }
        if (!empty($warning)) {
            $count = Db::name('archives')->alias('a')
                ->join('product_spec_value b', 'a.aid = b.aid', 'LEFT')
                ->where($warning)
                ->where($condition)
                ->where($stypeidwhere)
                ->group('a.aid')
                ->count('a.aid');
        } else {
            $count = Db::name('archives')->alias('a')
                ->where($condition)
                ->where($stypeidwhere)
                ->count('aid');
        }
        $Page = new Page($count, config('paginate.list_rows'));
        $list = [];
        if (!empty($count)) {
            $limit = $count > config('paginate.list_rows') ? $Page->firstRow.','.$Page->listRows : $count;
            if (!empty($warning)) {
                $list = Db::name('archives')
                    ->field("a.aid")
                    ->alias('a')
                    ->join('product_spec_value b', 'a.aid = b.aid', 'LEFT')
                    ->where($warning)
                    ->where($condition)
                    ->where($stypeidwhere)
                    ->order($orderby)
                    ->limit($limit)
                    ->group('a.aid')
                    ->getAllWithIndex('aid');
            } else {
                $list = Db::name('archives')
                    ->field("a.aid")
                    ->alias('a')
                    ->where($condition)
                    ->where($stypeidwhere)
                    ->order($orderby)
                    ->limit($limit)
                    ->getAllWithIndex('aid');
            }

            if (!empty($list)) {
                $aids = array_keys($list);
                $fields = "b.*, a.*, a.aid as aid";
                $row = Db::name('archives')
                    ->field($fields)
                    ->alias('a')
                    ->join('__ARCTYPE__ b', 'a.typeid = b.id', 'LEFT')
                    ->where(['a.aid'=>['IN', $aids]])
                    ->getAllWithIndex('aid');
                $productLabelList = Db::name('product_label')->alias('a')
                    ->field('a.aid, b.types_names')
                    ->join('label_types b', 'a.types_id=b.types_id','LEFT')
                    ->where(['aid'=>['IN', $aids], 'types_value'=>1])
                    ->getAllWithIndex('aid');
                foreach ($list as $key => $val) {
                    $row[$val['aid']]['arcurl'] = get_arcurl($row[$val['aid']]);
                    $row[$val['aid']]['litpic'] = get_default_pic($row[$val['aid']]['litpic']);
                    $row[$val['aid']]['label_names'] = !empty($productLabelList[$val['aid']]) ? $productLabelList[$val['aid']]['types_names'] : '';
                    $list[$key] = $row[$val['aid']];
                }
            }
        }
        
        // 查询预警商品的库存，多规格则取库存最低的规格库存
        if (!empty($param['query']) && 3 === intval($param['query'])) {
            $specStocks = Db::name('product_spec_value')->field('aid, spec_stock')->where(['aid'=>['IN', $aids]])->select();
            $specStocks = !empty($specStocks) ? group_same_key($specStocks, 'aid') : [];
            foreach ($specStocks as $key => $value) {
                $value = get_arr_column($value, 'spec_stock');
                $value = min($value);
                $specStocks[$key] = $value;
            }
            foreach ($list as $key => $value) {
                $value['stock_count'] = isset($specStocks[$key]) ? intval($specStocks[$key]) : $value['stock_count'];
                $list[$key] = $value;
            }
        }
        $assign_data = [];
        $assign_data['param'] = $param;
        $assign_data['page'] = $Page->show();
        $assign_data['list'] = $list;
        $assign_data['pager'] = $Page;
        $assign_data['tab'] = input('param.tab/d', 3);// 选项卡
        $assign_data['arctype_html'] = allow_release_arctype($typeid, array($this->channeltype)); // 允许发布文档列表的栏目
        $assign_data['arctype_info'] = $typeid > 0 ? Db::name('arctype')->field('typename')->find($typeid) : [];// 当前栏目信息
        // 是否存在产品模型的栏目
        $where = ['current_channel' => 2, 'is_del' => 0];
        $assign_data['is_product_arctype'] = Db::name('arctype')->where($where)->count();
        $this->assign($assign_data);
        $recycle_switch = tpSetting('recycle.recycle_switch');//回收站开关
        $this->assign('recycle_switch', $recycle_switch);

        // 商品标签查询
        $where = [
            'types_value' => 1,
        ];
        $labelTypes = Db::name('label_types')->where($where)->order('sort_order asc, types_id desc')->select();
        $this->assign('labelTypes', $labelTypes);

        // 目录列表
        $where = [
            'is_del' => 0,
            'current_channel' => 2
        ];
        $arctype_list = $this->arctypeLogic->arctype_list(0, 0, false, 0, $where, false);
        foreach ($arctype_list as $key => $value) {
            $value['litpic'] = get_default_pic($value['litpic']);
            $arctype_list[$key] = $value;
        }
        $this->assign('arctype_list', $arctype_list);

        // 获取所有有子栏目的栏目id
        $where = [
            'is_del' => 0,
            'parent_id' => ['gt', 0]
        ];
        $parent_ids = Db::name('arctype')->where($where)->group('parent_id')->cache(true, EYOUCMS_CACHE_TIME, 'arctype')->column('parent_id');
        $cookied_treeclicked =  json_decode(cookie('stypeid-treeClicked-Arr'));
        empty($cookied_treeclicked) && $cookied_treeclicked = [];
        $all_treeclicked = cookie('stypeid-treeClicked_All');
        empty($all_treeclicked) && $all_treeclicked = [];
        $tree = [
            'has_children'=>!empty($parent_ids) ? 1 : 0,
            'parent_ids'=>json_encode($parent_ids),
            'all_treeclicked'=>$all_treeclicked,
            'cookied_treeclicked'=>$cookied_treeclicked,
            'cookied_treeclicked_arr'=>json_encode($cookied_treeclicked),
        ];
        $this->assign('tree', $tree);

        return $this->fetch();
    }

    // 商品添加
    public function add()
    {
        $admin_info = session('admin_info');
        $auth_role_info = $admin_info['auth_role_info'];
        $this->assign('auth_role_info', $auth_role_info);
        $this->assign('admin_info', $admin_info);

        // 商品返回页面处理
        $goodsReturnUrl = '';
        $source = input('source/s', '');
        if (!empty($source) && 'add' == $source) {
            $goodsReturnUrl = $_SERVER['HTTP_REFERER'];
            session('goodsReturnUrl', $goodsReturnUrl);
        } else if (!empty($source) && 'save' == $source) {
            $goodsReturnUrl = session('goodsReturnUrl');
        }

        if (IS_POST) {
            $post = input('post.');

            /* 处理TAG标签 */
            // if (!empty($post['tags_new'])) {
            //     $post['tags'] = !empty($post['tags']) ? $post['tags'] . ',' . $post['tags_new'] : $post['tags_new'];
            //     unset($post['tags_new']);
            // }
            // $post['tags'] = explode(',', $post['tags']);
            // $post['tags'] = array_unique($post['tags']);
            // $post['tags'] = implode(',', $post['tags']);
            /* END */

            // 内容详情
            $content = input('post.addonFieldExt.content_ey_m', '', null);

            // 根据标题自动提取相关的关键字
            $seo_keywords = $post['seo_keywords'];
            if (!empty($seo_keywords)) {
                $seo_keywords = str_replace('，', ',', $seo_keywords);
            } else {
                // $seo_keywords = get_split_word($post['title'], $content);
            }

            // 自动获取内容第一张图片作为封面图
            // $is_remote = !empty($post['is_remote']) ? $post['is_remote'] : 0;
            // $litpic    = '';
            // if ($is_remote == 1) {
            //     $litpic = $post['litpic_remote'];
            // } else {
            //     $litpic = $post['litpic_local'];
            // }

            // 自动获取内容第一张图片作为封面图
            if (empty($post['litpic'])) $post['litpic'] = get_html_first_imgurl($content);

            /*是否有封面图*/
            if (empty($post['litpic'])) {
                $is_litpic = 0; // 无封面图
            } else {
                $is_litpic = 1; // 有封面图
            }

            // SEO描述
            $seo_description = '';
            if (empty($post['seo_description']) && !empty($content)) {
                $seo_description = @msubstr(checkStrHtml($content), 0, config('global.arc_seo_description_length'), false);
            } else {
                $seo_description = $post['seo_description'];
            }

            // 外部链接跳转
            // $jumplinks = '';
            // $is_jump   = isset($post['is_jump']) ? $post['is_jump'] : 0;
            // if (intval($is_jump) > 0) {
            //     $jumplinks = $post['jumplinks'];
            // }

            // 模板文件，如果文档模板名与栏目指定的一致，默认就为空。让它跟随栏目的指定而变
            if ($post['type_tempview'] == $post['tempview']) {
                unset($post['type_tempview']);
                unset($post['tempview']);
            }

            //处理自定义文件名,仅由字母数字下划线和短横杆组成,大写强制转换为小写
            $htmlfilename = trim($post['htmlfilename']);
            if (!empty($htmlfilename)) {
                $htmlfilename = preg_replace("/[^a-zA-Z0-9_-]+/", "-", $htmlfilename);
                $htmlfilename = strtolower($htmlfilename);
                //判断是否存在相同的自定义文件名
                $filenameCount = Db::name('archives')->where([
                    'htmlfilename' => $htmlfilename,
                ])->count();
                if (!empty($filenameCount)) {
                    $this->error("自定义文件名已存在，请重新设置！");
                }
            }
            $post['htmlfilename'] = $htmlfilename;

            // 产品类型
            // if (!empty($post['prom_type'])) {
            //     if ($post['prom_type_vir'] == 2) {
            //         $post['netdisk_url'] = trim($post['netdisk_url']);
            //         if (empty($post['netdisk_url'])) {
            //             $this->error("网盘地址不能为空！");
            //         }
            //         $post['prom_type'] = 2;
            //     } else if ($post['prom_type_vir'] == 3) {
            //         $post['text_content'] = trim($post['text_content']);
            //         if (empty($post['text_content'])) {
            //             $this->error("虚拟文本内容不能为空！");
            //         }
            //         $post['prom_type'] = 3;
            //     }
            // }

            //做自动通过审核判断
            // if ($admin_info['role_id'] > 0 && $auth_role_info['check_oneself'] < 1) {
            //     $post['arcrank'] = -1;
            // }
            
            // 副栏目
            // if (isset($post['stypeid'])) {
            //     $post['stypeid'] = preg_replace('/([^\d\,\，]+)/i', ',', $post['stypeid']);
            //     $post['stypeid'] = str_replace('，', ',', $post['stypeid']);
            //     $post['stypeid'] = trim($post['stypeid'], ',');
            //     $post['stypeid'] = str_replace(",{$post['typeid']},", ',', ",{$post['stypeid']},");
            //     // $post['stypeid'] = trim($post['stypeid'], ',');
            // }
            // 分类ID组处理
            $stypeid = [];
            $select_ids = !empty($post['select_ids']) ? $post['select_ids'] : [];
            foreach ($select_ids as $key => $value) {
                if (!empty($value)) {
                    // 取出每个分类ID数组最后一个值装入指定数组
                    $value = explode(',', $value);
                    array_push($stypeid, array_pop($value));
                }
            }
            $post['stypeid'] = !empty($stypeid) ? ',' . implode(',', array_unique($stypeid)) . ',' : '';

            // --存储数据
            $newData = array(
                'channel'         => $this->channeltype,
                'is_b'            => empty($post['is_b']) ? 0 : $post['is_b'],
                'is_head'         => empty($post['is_head']) ? 0 : $post['is_head'],
                'is_special'      => empty($post['is_special']) ? 0 : $post['is_special'],
                'is_recom'        => empty($post['is_recom']) ? 0 : $post['is_recom'],
                'is_roll'         => empty($post['is_roll']) ? 0 : $post['is_roll'],
                'is_slide'        => empty($post['is_slide']) ? 0 : $post['is_slide'],
                'is_diyattr'      => empty($post['is_diyattr']) ? 0 : $post['is_diyattr'],
                'is_jump'         => empty($is_jump) ? 0 : $is_jump,
                'is_litpic'       => empty($is_litpic) ? 0 : $is_litpic,
                'jumplinks'       => empty($jumplinks) ? 0 : $jumplinks,
                'origin'          => empty($post['origin']) ? '网络' : $post['origin'],
                'seo_keywords'    => empty($seo_keywords) ? '' : $seo_keywords,
                'seo_description' => empty($seo_description) ? '' : $seo_description,
                'admin_id'        => session('admin_info.admin_id'),
                'stock_show'      => empty($post['stock_show']) ? 0 : $post['stock_show'],
                'users_price'     => empty($post['users_price']) ? 0 : floatval($post['users_price']),
                'sort_order'      => 100,
                'add_time'        => strtotime($post['add_time']),
                'update_time'     => strtotime($post['add_time']),
            );
            $data = array_merge($post, $newData);
            $data['goods_unit'] = !empty($this->usersConfig['goods_unit']) ? $this->usersConfig['goods_unit'] : '件';
            $aid = Db::name('archives')->insertGetId($data);
            $_POST['aid'] = $aid;
            if (!empty($aid)) {
                // ---------后置操作
                model('Product')->afterSave($aid, $data, 'add', true);

                // 添加查询执行语句到mysql缓存表
                model('SqlCacheTable')->InsertSqlCacheTable();

                // 若选择多规格选项，则添加产品规格
                if (!empty($post['goods_spec']) && 2 == $post['goods_spec']) {
                    // 更新规格名称数据
                    $data['aid'] = $aid;
                    model('ProductSpecData')->ProducSpecNameEditSave($data, 'add');
                    // 更新规格值及金额数据
                    model('ProductSpecValue')->ProducSpecValueEditSave($data, 'add');
                }

                // 商品标签处理
                $productLabel = [];
                foreach ($data['marketing'] as $key => $value) {
                    if ($value) {
                        $productLabel[] = [
                            'aid' => intval($aid),
                            'types_id' => intval($value),
                            'sort_order' => intval($key),
                            'add_time' => getTime(),
                            'update_time' => getTime(),
                        ];
                    }
                }
                foreach ($data['service'] as $key => $value) {
                    if ($value) {
                        $productLabel[] = [
                            'aid' => intval($aid),
                            'types_id' => intval($value),
                            'sort_order' => intval($key),
                            'add_time' => getTime(),
                            'update_time' => getTime(),
                        ];
                    }
                }
                if (!empty($productLabel)) Db::name('product_label')->insertAll($productLabel);

                // 若选择自定义参数则执行
                if (!empty($post['attr_name']) && !empty($post['attr_value'])) {
                    // 新增商品参数
                    $attrName = !empty($post['attr_name']) ? $post['attr_name'] : [];
                    $attrValue = !empty($post['attr_value']) ? $post['attr_value'] : [];
                    $sortOrder = !empty($post['sort_order']) ? $post['sort_order'] : 100;
                    $productAttribute = $productAttr = [];
                    $time = getTime();
                    foreach ($attrName as $key => $value) {
                        if (!empty($value)) {
                            $productAttribute = [
                                'aid' => $aid,
                                'attr_name' => trim($value),
                                'attr_values' => '',
                                'sort_order' => 100,
                                'is_custom' => 1,
                                'add_time' => $time,
                                'update_time' => $time,
                            ];
                            $attrID = Db::name('shop_product_attribute')->insertGetId($productAttribute);
                            if (!empty($attrValue[$key])) {
                                $productAttr = [
                                    'aid' => $aid,
                                    'attr_id' => $attrID,
                                    'attr_value' => $attrValue[$key],
                                    'is_custom' => 1,
                                    'sort_order' => intval($sortOrder[$key]),
                                    'add_time' => $time,
                                    'update_time' => $time,
                                ];
                                Db::name('shop_product_attr')->insertGetId($productAttr);
                            }
                        }
                    }
                }

                // if (!empty($post['param_type']) && 2 == $post['param_type']) {
                //     // 强制删除商品参数
                //     $where = [
                //         'aid' => $aid,
                //     ];
                //     Db::name('product_custom_param')->where($where)->delete(true);
                //     // 新增商品参数
                //     $paramName = !empty($post['param_name']) ? $post['param_name'] : [];
                //     $paramValue = !empty($post['param_value']) ? $post['param_value'] : [];
                //     if (!empty($paramName) && !empty($paramValue)) {
                //         $insertAll = [];
                //         $time = getTime();
                //         foreach ($paramName as $key => $value) {
                //             if (!empty($value) && !empty($paramValue[$key])) {
                //                 $insertAll[] = [
                //                     'aid' => $aid,
                //                     'param_name' => $value,
                //                     'param_value' => $paramValue[$key],
                //                     'sort_order' => $key,
                //                     'add_time' => $time,
                //                     'update_time' => $time,
                //                 ];
                //             }
                //         }
                //         if (!empty($insertAll)) Db::name('product_custom_param')->insertAll($insertAll);
                //     }
                // }
                //写入统计
                eyou_statistics_data(6);

                adminLog('新增产品：' . $data['title']);

                // 虚拟商品保存
                // if (!empty($post['prom_type']) && in_array($post['prom_type'], [2, 3])) {
                //     model('ProductNetdisk')->saveProductNetdisk($aid, $data);
                // }

                // 生成静态页面代码
                $successData = [
                    'aid' => $aid,
                    'tid' => $post['typeid'],
                ];
                // 存在指定返回页面则执行
                $goodsReturnUrl = !empty($goodsReturnUrl) ? $goodsReturnUrl : url('ShopProduct/index');
                $this->success("操作成功!", $goodsReturnUrl, $successData);
            }
            $this->error("操作失败!");
        }

        $typeid = input('param.typeid/d', 0);
        $assign_data['typeid'] = $typeid; // 栏目ID

        // 栏目信息
        $arctypeInfo = Db::name('arctype')->find($typeid);

        // 允许发布文档列表的栏目
        // $assign_data['arctype_html'] = allow_release_arctype($typeid, array($this->channeltype), false);
        // $assign_data['arctypeList'] = allow_release_arctype($typeid, array($this->channeltype), false);
        
        // 查询商品分类
        $where = [
            'is_del' => 0,
            'status' => 1,
            'is_hidden' => 0,
            'current_channel' => $this->channeltype,
        ];
        $arctypeList = Db::name('arctype')->field('id, parent_id, topid, typename, grade')->where($where)->select();
        // 整合商品分类下级
        $arctypeSublist = [];
        foreach ($arctypeList as $key => $val) {
            if (!empty($val['parent_id'])) $arctypeSublist[$val['parent_id']][] = $val['id'];
        }
        // 统计商品分类下级数量
        foreach ($arctypeList as $key => $val) { 
            $arctypeList[$key]['has_children'] = !empty($arctypeSublist[$val['id']]) ? count($arctypeSublist[$val['id']]) : 0;
        }
        // 分类数据处理
        $arctypeList = group_same_key($arctypeList, 'grade');
        $arctypeList[0] = !empty($arctypeList[0]) ? convert_arr_key($arctypeList[0], 'id') : [];
        $arctypeList[1] = !empty($arctypeList[1]) ? convert_arr_key($arctypeList[1], 'id') : [];
        $arctypeList[2] = !empty($arctypeList[2]) ? convert_arr_key($arctypeList[2], 'id') : [];
        foreach ($arctypeList as $key => $value) {
            foreach ($value as $key_1 => $value_1) {
                if (0 === intval($key)) {
                    $value_1['ids'] = $value_1['id'];
                    $value_1['names'] = $value_1['typename'];
                } else if (1 === intval($key)) {
                    $parentID = $arctypeList[0][$value_1['parent_id']]['id'];
                    $value_1['ids'] = $parentID . ',' . $value_1['id'];
                    $parentName = $arctypeList[0][$value_1['parent_id']]['typename'];
                    $value_1['names'] = $parentName . '->' . $value_1['typename'];
                } else if (2 === intval($key)) {
                    $topID = $arctypeList[0][$value_1['topid']]['id'];
                    $parentID = $arctypeList[1][$value_1['parent_id']]['id'];
                    $value_1['ids'] = $topID . ',' . $parentID . ',' . $value_1['id'];
                    $topName = $arctypeList[0][$value_1['topid']]['typename'];
                    $parentName = $arctypeList[1][$value_1['parent_id']]['typename'];
                    $value_1['names'] = $topName . '->' . $parentName . '->' . $value_1['typename'];
                }
                $value[$key_1] = $value_1;
            }
            $arctypeList[$key] = $value;
        }
        $assign_data['arctypeList'] = $arctypeList;

        // 可控制的字段列表
        $assign_data['ifcontrolRow'] = Db::name('channelfield')->field('id,name')->where([
            'channel_id' => $this->channeltype,
            'ifmain'     => 1,
            'ifeditable' => 1,
            'ifcontrol'  => 0,
            'status'     => 1,
        ])->getAllWithIndex('name');

        // 阅读权限
        // $assign_data['arcrank_list'] = get_arcrank_list();

        // 产品参数
        // $assign_data['canshu'] = $this->ajax_get_attr_input($typeid);

        // 模板列表
        $archivesLogic = new \app\admin\logic\ArchivesLogic;
        $assign_data['templateList'] = $archivesLogic->getTemplateList($this->nid);

        // 默认模板文件
        $tempview = 'view_' . $this->nid . '.' . config('template.view_suffix');
        !empty($arctypeInfo['tempview']) && $tempview = $arctypeInfo['tempview'];
        $assign_data['tempview'] = $tempview;

        // 商城配置
        $shopConfig = getUsersConfigData('shop');
        $assign_data['shopConfig'] = $shopConfig;

        // 商品规格
        if (isset($shopConfig['shop_open_spec']) && 1 == $shopConfig['shop_open_spec']) {
            // 清除处理表的aid
            session('handleAID', 0);
            // 删除商品添加时产生的废弃规格
            $del_spec = session('del_spec') ? session('del_spec') : [];
            if (!empty($del_spec)) {
                $del_spec = array_unique($del_spec);
                $where = [
                    'spec_mark_id' => ['IN', $del_spec]
                ];
                Db::name('product_spec_data_handle')->where($where)->delete(true);
                // 清除 session
                session('del_spec', null);
            }
            // 预设值名称
            // $assign_data['preset_value'] = Db::name('product_spec_preset')->field('preset_id, preset_mark_id, preset_name')->group('preset_mark_id')->order('preset_mark_id desc')->select();
            // 读取规格预设库最大参数标记ID
            // $maxPresetMarkID = $assign_data['preset_value'][0]['preset_mark_id'];
            // $assign_data['maxPresetMarkID'] = $maxPresetMarkID + 1;
        }

        // 商品参数列表
        $where = [
            'status' => 1,
            'is_del' => 0,
        ];
        $assign_data['AttrList'] = $this->shop_product_attrlist_db->where($where)->order('sort_order asc, list_id asc')->select();

        // 最大参数属性ID值 +1
        $maxAttrID = Db::name('shop_product_attribute')->max('attr_id');
        $assign_data['maxAttrID'] = ++$maxAttrID;

        // URL模式
        $tpcache = config('tpcache');
        $assign_data['seo_pseudo'] = !empty($tpcache['seo_pseudo']) ? $tpcache['seo_pseudo'] : 1;

        // 文档默认浏览量
        // $globalConfig = tpCache('global');
        // if (isset($globalConfig['other_arcclick']) && 0 <= $globalConfig['other_arcclick']) {
        //     $arcclick_arr = explode("|", $globalConfig['other_arcclick']);
        //     if (count($arcclick_arr) > 1) {
        //         $assign_data['rand_arcclick'] = mt_rand($arcclick_arr[0], $arcclick_arr[1]);
        //     } else {
        //         $assign_data['rand_arcclick'] = intval($arcclick_arr[0]);
        //     }
        // } else {
        //     $arcclick_config['other_arcclick'] = '500|1000';
        //     tpCache('other', $arcclick_config);
        //     $assign_data['rand_arcclick'] = mt_rand(500, 1000);
        // }

        // 文档属性
        $assign_data['archives_flags'] = model('ArchivesFlag')->getList();
        $channelRow = Db::name('channeltype')->where('id', $this->channeltype)->find();
        $assign_data['channelRow'] = $channelRow;

        // 来源列表
        // $system_originlist = tpSetting('system.system_originlist');
        // $system_originlist = json_decode($system_originlist, true);
        // $system_originlist = !empty($system_originlist) ? $system_originlist : [];
        // $assign_data['system_originlist_str'] = implode(PHP_EOL, $system_originlist);
        // $assign_data['system_originlist_0'] = !empty($system_originlist) ? $system_originlist[0] : "";
        // 多站点，当用站点域名访问后台，发布文档自动选择当前所属区域
        model('Citysite')->auto_location_select($assign_data);

        // 标签列表
        $where = [
            'status' => 1,
        ];
        $labelTypes = Db::name('label_types')->where($where)->order('sort_order asc, types_id desc')->select();
        $serviceLabel = $marketingLabel = [];
        foreach ($labelTypes as $key => $value) {
            if (1 === intval($value['types_value'])) {
                array_push($marketingLabel, $value);
            } else if (2 === intval($value['types_value'])) {
                array_push($serviceLabel, $value);
            }
        }
        $this->assign('serviceLabel', $serviceLabel);
        $this->assign('marketingLabel', $marketingLabel);

        // 视频类型
        $media_type = tpCache('basic.media_type');
        $media_type = !empty($media_type) ? $media_type : config('global.media_ext');
        $media_type = str_replace(",", "|", $media_type);
        $this->assign('media_type', $media_type);

        // 系统最大上传视频的大小
        $this->assign('upload_max_filesize', upload_max_filesize());

        // 商城运费模板
        $where = [
            'status' => 1,
        ];
        $template = Db::name('shop_freight_template')->field('template_id, template_name, default')->where($where)->select();
        $this->assign('template', $template);

        $this->assign($assign_data);
        return $this->fetch();
    }

    // 商品编辑
    public function edit()
    {
        $admin_info = session('admin_info');
        $auth_role_info = $admin_info['auth_role_info'];
        $this->assign('auth_role_info', $auth_role_info);
        $this->assign('admin_info', $admin_info);

        // 商品返回页面处理
        $goodsReturnUrl = '';
        $source = input('source/s', '');
        if (!empty($source) && 'add' == $source) {
            $goodsReturnUrl = $_SERVER['HTTP_REFERER'];
            session('goodsReturnUrl', $goodsReturnUrl);
        } else if (!empty($source) && 'save' == $source) {
            $goodsReturnUrl = session('goodsReturnUrl');
        }

        if (IS_POST) {
            $post = input('post.');
            $post['aid'] = intval($post['aid']);

            /* 处理TAG标签 */
            // if (!empty($post['tags_new'])) {
            //     $post['tags'] = !empty($post['tags']) ? $post['tags'] . ',' . $post['tags_new'] : $post['tags_new'];
            //     unset($post['tags_new']);
            // }
            // $post['tags'] = explode(',', $post['tags']);
            // $post['tags'] = array_unique($post['tags']);
            // $post['tags'] = implode(',', $post['tags']);
            /* END */

            $typeid  = input('post.typeid/d', 0);
            $content = input('post.addonFieldExt.content_ey_m', '', null);

            // 根据标题自动提取相关的关键字
            $seo_keywords = $post['seo_keywords'];
            if (!empty($seo_keywords)) {
                $seo_keywords = str_replace('，', ',', $seo_keywords);
            } else {
                // $seo_keywords = get_split_word($post['title'], $content);
            }

            // 自动获取内容第一张图片作为封面图
            // $is_remote = !empty($post['is_remote']) ? $post['is_remote'] : 0;
            // $litpic    = '';
            // if ($is_remote == 1) {
            //     $litpic = $post['litpic_remote'];
            // } else {
            //     $litpic = $post['litpic_local'];
            // }

            // 自动获取内容第一张图片作为封面图
            if (empty($post['litpic'])) $post['litpic'] = get_html_first_imgurl($content);

            /*是否有封面图*/
            if (empty($post['litpic'])) {
                $is_litpic = 0; // 无封面图
            } else {
                $is_litpic = !empty($post['is_litpic']) ? $post['is_litpic'] : 0; // 有封面图
            }

            // SEO描述
            $seo_description = '';
            if (empty($post['seo_description']) && !empty($content)) {
                $seo_description = @msubstr(checkStrHtml($content), 0, config('global.arc_seo_description_length'), false);
            } else {
                $seo_description = $post['seo_description'];
            }

            // --外部链接
            // $jumplinks = '';
            // $is_jump   = isset($post['is_jump']) ? $post['is_jump'] : 0;
            // if (intval($is_jump) > 0) {
            //     $jumplinks = $post['jumplinks'];
            // }

            // 模板文件，如果文档模板名与栏目指定的一致，默认就为空。让它跟随栏目的指定而变
            if ($post['type_tempview'] == $post['tempview']) {
                unset($post['type_tempview']);
                unset($post['tempview']);
            }

            // 产品类型
            // if (!empty($post['prom_type'])) {
            //     if ($post['prom_type_vir'] == 2) {
            //         $post['netdisk_url'] = trim($post['netdisk_url']);
            //         if (empty($post['netdisk_url'])) {
            //             $this->error("网盘地址不能为空！");
            //         }
            //         $post['prom_type'] = 2;
            //     } else if ($post['prom_type_vir'] == 3) {
            //         $post['text_content'] = trim($post['text_content']);
            //         if (empty($post['text_content'])) {
            //             $this->error("虚拟文本内容不能为空！");
            //         }
            //         $post['prom_type'] = 3;
            //     }
            // }

            //处理自定义文件名,仅由字母数字下划线和短横杆组成,大写强制转换为小写
            $htmlfilename = trim($post['htmlfilename']);
            if (!empty($htmlfilename)) {
                $htmlfilename = preg_replace("/[^a-zA-Z0-9_-]+/", "-", $htmlfilename);
                $htmlfilename = strtolower($htmlfilename);
                //判断是否存在相同的自定义文件名
                $filenameCount = Db::name('archives')->where([
                    'aid'          => ['NEQ', $post['aid']],
                    'htmlfilename' => $htmlfilename,
                ])->count();
                if (!empty($filenameCount)) {
                    $this->error("自定义文件名已存在，请重新设置！");
                }
            }
            $post['htmlfilename'] = $htmlfilename;

            // 同步栏目切换模型之后的文档模型
            $channel = Db::name('arctype')->where(['id' => $typeid])->getField('current_channel');

            // 做未通过审核文档不允许修改文档状态操作
            // if ($admin_info['role_id'] > 0 && $auth_role_info['check_oneself'] < 1) {
            //     $old_archives_arcrank = Db::name('archives')->where(['aid' => $post['aid']])->getField("arcrank");
            //     if ($old_archives_arcrank < 0) {
            //         unset($post['arcrank']);
            //     }
            // }

            // 副栏目
            // if (isset($post['stypeid'])) {
            //     $post['stypeid'] = preg_replace('/([^\d\,\，]+)/i', ',', $post['stypeid']);
            //     $post['stypeid'] = str_replace('，', ',', $post['stypeid']);
            //     $post['stypeid'] = trim($post['stypeid'], ',');
            //     $post['stypeid'] = str_replace(",{$typeid},", ',', ",{$post['stypeid']},");
            //     // $post['stypeid'] = trim($post['stypeid'], ',');
            // }
            // 分类ID组处理
            $stypeid = [];
            $select_ids = !empty($post['select_ids']) ? $post['select_ids'] : [];
            foreach ($select_ids as $key => $value) {
                if (!empty($value)) {
                    // 取出每个分类ID数组最后一个值装入指定数组
                    $value = explode(',', $value);
                    array_push($stypeid, array_pop($value));
                }
            }
            $post['stypeid'] = !empty($stypeid) ? ',' . implode(',', array_unique($stypeid)) . ',' : '';

            // --存储数据
            $newData = array(
                'typeid'  => !empty($typeid) ? intval($typeid) : 0,
                'channel' => !empty($channel) ? intval($channel) : 2,
                'is_litpic'       => $is_litpic,
                'seo_keywords'    => $seo_keywords,
                'seo_description' => $seo_description,
                'stock_show' => !empty($post['stock_show']) ? intval($post['stock_show']) : 0,
                'users_price' => !empty($post['users_price']) ? floatval($post['users_price']) : 0,
                'add_time'        => strtotime($post['add_time']),
                'update_time' => getTime(),
            );
            $data = array_merge($post, $newData);
            // 更新商品信息
            $where = [
                'aid'  => $data['aid'],
            ];
            $result = Db::name('archives')->where($where)->update($data);
            if (!empty($result)) {
                // ---------后置操作
                model('Product')->afterSave($data['aid'], $data, 'edit', true);

                // 虚拟商品保存
                if (!empty($post['prom_type']) && in_array($post['prom_type'], [2, 3])) {
                    model('ProductNetdisk')->saveProductNetdisk($data['aid'], $data);
                }

                // 若选择单规格则清理多规格数据
                if (!empty($post['goods_spec']) && 1 == $post['goods_spec']) {
                    // 产品规格数据表
                    Db::name("product_spec_data")->where('aid', $data['aid'])->delete();
                    // 产品多规格组装表
                    Db::name("product_spec_value")->where('aid', $data['aid'])->delete();
                    // 产品规格数据处理表
                    Db::name("product_spec_data_handle")->where('aid', $data['aid'])->delete();
                }
                // 若选择多规格选项，则添加产品规格
                else if (!empty($post['goods_spec']) && 2 == $post['goods_spec']) {
                    // 更新规格名称数据
                    model('ProductSpecData')->ProducSpecNameEditSave($data);
                    // 更新规格值及金额数据
                    model('ProductSpecValue')->ProducSpecValueEditSave($data);
                }

                // 商品标签处理
                $productLabel = [];
                foreach ($data['marketing'] as $key => $value) {
                    if ($value) {
                        $productLabel[] = [
                            'aid' => intval($data['aid']),
                            'types_id' => intval($value),
                            'sort_order' => intval($key),
                            'add_time' => getTime(),
                            'update_time' => getTime(),
                        ];
                    }
                }
                foreach ($data['service'] as $key => $value) {
                    if ($value) {
                        $productLabel[] = [
                            'aid' => intval($data['aid']),
                            'types_id' => intval($value),
                            'sort_order' => intval($key),
                            'add_time' => getTime(),
                            'update_time' => getTime(),
                        ];
                    }
                }
                if (!empty($productLabel)) {
                    Db::name('product_label')->where(['aid'=>$data['aid']])->delete(true);
                    Db::name('product_label')->insertAll($productLabel);
                }

                // 若选择自定义参数则执行
                if (!empty($post['attr_name']) && !empty($post['attr_value'])) {
                    // 新增商品参数
                    $attrName = !empty($post['attr_name']) ? $post['attr_name'] : [];
                    $attrValue = !empty($post['attr_value']) ? $post['attr_value'] : [];
                    $sortOrder = !empty($post['sort_order']) ? $post['sort_order'] : 100;
                    $productAttribute = $productAttr = [];
                    $time = getTime();
                    foreach ($attrName as $key => $value) {
                        if (!empty($value)) {
                            $productAttribute = [
                                'aid' => $post['aid'],
                                'attr_name' => trim($value),
                                'attr_values' => '',
                                'sort_order' => 100,
                                'is_custom' => 1,
                                'add_time' => $time,
                                'update_time' => $time,
                            ];
                            $attrID = Db::name('shop_product_attribute')->insertGetId($productAttribute);
                            if (!empty($attrValue[$key])) {
                                $productAttr = [
                                    'aid' => $post['aid'],
                                    'attr_id' => $attrID,
                                    'attr_value' => $attrValue[$key],
                                    'is_custom' => 1,
                                    'sort_order' => intval($sortOrder[$key]),
                                    'add_time' => $time,
                                    'update_time' => $time,
                                ];
                                Db::name('shop_product_attr')->insertGetId($productAttr);
                            }
                        }
                    }
                }
                // 删除指定的商品参数
                if (!empty($post['del_attr_id'])) {
                    $delAttrID = explode(',', $post['del_attr_id']);
                    $where = [
                        'is_custom' => 1,
                        'attr_id' => ['IN', $delAttrID]
                    ];
                    Db::name('shop_product_attr')->where($where)->delete(true);
                    Db::name('shop_product_attribute')->where($where)->delete(true);
                }
                // if (!empty($post['param_type']) && 2 == $post['param_type']) {
                //     // 强制删除商品参数
                //     $where = [
                //         'aid' => $data['aid'],
                //     ];
                //     Db::name('product_custom_param')->where($where)->delete(true);
                //     // 新增商品参数
                //     $paramName = !empty($post['param_name']) ? $post['param_name'] : [];
                //     $paramValue = !empty($post['param_value']) ? $post['param_value'] : [];
                //     if (!empty($paramName) && !empty($paramValue)) {
                //         $insertAll = [];
                //         $time = getTime();
                //         foreach ($paramName as $key => $value) {
                //             if (!empty($value) && !empty($paramValue[$key])) {
                //                 $insertAll[] = [
                //                     'aid' => $data['aid'],
                //                     'param_name' => $value,
                //                     'param_value' => $paramValue[$key],
                //                     'sort_order' => $key,
                //                     'add_time' => $time,
                //                     'update_time' => $time,
                //                 ];
                //             }
                //         }
                //         if (!empty($insertAll)) Db::name('product_custom_param')->insertAll($insertAll);
                //     }
                // }

                adminLog('编辑产品：' . $data['title']);

                // 生成静态页面代码
                $successData = [
                    'aid' => $data['aid'],
                    'tid' => $typeid,
                ];
                // 存在指定返回页面则执行
                $goodsReturnUrl = !empty($goodsReturnUrl) ? $goodsReturnUrl : url('ShopProduct/index');
                $this->success("操作成功!", $goodsReturnUrl, $successData);
            }
            $this->error("操作失败!");
        }

        $assign_data = array();

        $id = input('id/d', 0);
        $info = model('Product')->getInfo($id);
        if (empty($info)) $this->error('数据不存在，请联系管理员！');

        $stypeid = !empty($info['stypeid']) ? $info['stypeid'] : '';
        $arctypeName = [];
        if (!empty($stypeid)) {
            $stypeid = array_unique(array_filter(explode(',', $stypeid)));
            $where = [
                'a.id' => ['IN', $stypeid]
            ];
            $field = 'c.id as oneID, c.typename as oneName, b.id as twoID, b.typename as twoName, a.id as threeID, a.typename as threeName';
            $arctypeName = Db::name('arctype')->alias('a')
                ->join('__ARCTYPE__ b', 'a.parent_id = b.id', 'LEFT')
                ->join('__ARCTYPE__ c', 'a.topid = c.id', 'LEFT')
                ->field($field)
                ->where($where)
                ->select();
            if (!empty($arctypeName)) {
                foreach ($arctypeName as $key => $value) {
                    $value['style'] = 0 === intval($key) ? 290 : 290 + (intval($key) * 47);
                    if (!empty($value['oneID'])) {
                        $value['ids'][] = $value['oneID'];
                        $value['names'][] = $value['oneName'];
                    }
                    if (intval($value['oneID']) !== intval($value['twoID']) && !empty($value['twoID'])) {
                        $value['ids'][] = $value['twoID'];
                        $value['names'][] = $value['twoName'];
                    }
                    if (!empty($value['threeID'])) {
                        $value['ids'][] = $value['threeID'];
                        $value['names'][] = $value['threeName'];
                    }
                    $value['ids'] = !empty($value['ids']) ? implode(',', $value['ids']) : '';
                    $value['names'] = !empty($value['names']) ? implode('->', $value['names']) : '';
                    $arctypeName[$key] = $value;
                }
            }
        }
        $this->assign('arctypeName', $arctypeName);

        // 获取规格数据信息
        // 包含：SpecSelectName、HtmlTable、spec_mark_id_arr、preset_value
        $assign_data = model('ProductSpecData')->GetProductSpecData($id);

        // 兼容采集没有归属栏目的文档
        if (empty($info['channel'])) {
            $channelRow = Db::name('channeltype')->field('id as channel')->where('id', $this->channeltype)->find();
            $info = array_merge($info, $channelRow);
        }

        // 栏目ID及栏目信息
        $typeid = $info['typeid'];
        $assign_data['typeid'] = $typeid;
        $arctypeInfo = Db::name('arctype')->find($typeid);
        $info['channel'] = $arctypeInfo['current_channel'];
        if (is_http_url($info['litpic'])) {
            $info['is_remote']     = 1;
            $info['litpic_remote'] = handle_subdir_pic($info['litpic']);
        } else {
            $info['is_remote']    = 0;
            $info['litpic_local'] = handle_subdir_pic($info['litpic']);
        }

        // 副栏目
        $stypeid_arr = [];
        if (!empty($info['stypeid'])) {
            $info['stypeid'] = trim($info['stypeid'], ',');
            $stypeid_arr = Db::name('arctype')->field('id,typename')->where(['id'=>['IN', $info['stypeid']],'is_del'=>0])->select();
        }
        $assign_data['stypeid_arr'] = $stypeid_arr;

        $assign_data['field'] = $info;

        // 产品相册
        $proimg_list = model('ProductImg')->getProImg($id);
        foreach ($proimg_list as $key => $val) {
            $proimg_list[$key]['image_url'] = handle_subdir_pic($val['image_url']); // 支持子目录
        }
        $assign_data['proimg_list'] = $proimg_list;

        // 允许发布文档列表的栏目，文档所在模型以栏目所在模型为主，兼容切换模型之后的数据编辑
        // $assign_data['arctype_html'] = allow_release_arctype($typeid, array($info['channel']));
        // 查询商品分类
        $where = [
            'is_del' => 0,
            'status' => 1,
            'is_hidden' => 0,
            'current_channel' => $this->channeltype,
        ];
        $arctypeList = Db::name('arctype')->field('id, parent_id, topid, typename, grade')->where($where)->select();
        // 整合商品分类下级
        $arctypeSublist = [];
        foreach ($arctypeList as $key => $val) {
            if (!empty($val['parent_id'])) $arctypeSublist[$val['parent_id']][] = $val['id'];
        }
        // 统计商品分类下级数量
        foreach ($arctypeList as $key => $val) { 
            $arctypeList[$key]['has_children'] = !empty($arctypeSublist[$val['id']]) ? count($arctypeSublist[$val['id']]) : 0;
        }
        // 分类数据处理
        $arctypeList = group_same_key($arctypeList, 'grade');
        $arctypeList[0] = !empty($arctypeList[0]) ? convert_arr_key($arctypeList[0], 'id') : [];
        $arctypeList[1] = !empty($arctypeList[1]) ? convert_arr_key($arctypeList[1], 'id') : [];
        $arctypeList[2] = !empty($arctypeList[2]) ? convert_arr_key($arctypeList[2], 'id') : [];
        foreach ($arctypeList as $key => $value) {
            foreach ($value as $key_1 => $value_1) {
                if (0 === intval($key)) {
                    $value_1['ids'] = $value_1['id'];
                    $value_1['names'] = $value_1['typename'];
                } else if (1 === intval($key)) {
                    $parentID = $arctypeList[0][$value_1['parent_id']]['id'];
                    $value_1['ids'] = $parentID . ',' . $value_1['id'];
                    $parentName = $arctypeList[0][$value_1['parent_id']]['typename'];
                    $value_1['names'] = $parentName . '->' . $value_1['typename'];
                } else if (2 === intval($key)) {
                    $topID = $arctypeList[0][$value_1['topid']]['id'];
                    $parentID = $arctypeList[1][$value_1['parent_id']]['id'];
                    $value_1['ids'] = $topID . ',' . $parentID . ',' . $value_1['id'];
                    $topName = $arctypeList[0][$value_1['topid']]['typename'];
                    $parentName = $arctypeList[1][$value_1['parent_id']]['typename'];
                    $value_1['names'] = $topName . '->' . $parentName . '->' . $value_1['typename'];
                }
                $value[$key_1] = $value_1;
            }
            $arctypeList[$key] = $value;
        }
        $assign_data['arctypeList'] = $arctypeList;

        // 可控制的主表字段列表
        $assign_data['ifcontrolRow'] = Db::name('channelfield')->field('id,name')->where([
            'channel_id' => $this->channeltype,
            'ifmain'     => 1,
            'ifeditable' => 1,
            'ifcontrol'  => 0,
            'status'     => 1,
        ])->getAllWithIndex('name');

        // 虚拟商品内容读取
        $assign_data['netdisk'] = Db::name("product_netdisk")->where('aid', $id)->find();

        // 阅读权限
        // $assign_data['arcrank_list'] = get_arcrank_list();

        // 模板列表
        $archivesLogic = new \app\admin\logic\ArchivesLogic;
        $templateList  = $archivesLogic->getTemplateList($this->nid);
        $assign_data['templateList'] = $templateList;

        // 默认模板文件
        $tempview = $info['tempview'];
        empty($tempview) && $tempview = $arctypeInfo['tempview'];
        $assign_data['tempview'] = $tempview;

        // 商城配置
        $shopConfig = getUsersConfigData('shop');
        $assign_data['shopConfig'] = $shopConfig;

        // 处理产品价格属性
        $IsSame = '';
        if (empty($shopConfig['shop_type']) || 1 == $shopConfig['shop_type']) {
            if ($shopConfig['shop_type'] == $assign_data['field']['prom_type']) {
                $IsSame = '0'; // 相同
            }elseif ($shopConfig['shop_type']==1 && in_array($assign_data['field']['prom_type'], [2,3])) {
                $IsSame = '0'; // 相同
            }else{
                $IsSame = '1'; // 不相同
            }
        }
        $assign_data['IsSame'] = $IsSame;

        // URL模式
        $tpcache = config('tpcache');
        $assign_data['seo_pseudo'] = !empty($tpcache['seo_pseudo']) ? $tpcache['seo_pseudo'] : 1;

        // 商品参数列表
        $where = [
            'status' => 1,
            'is_del' => 0,
        ];
        $assign_data['AttrList'] = $this->shop_product_attrlist_db->where($where)->order('sort_order asc, list_id asc')->select();

        // 商品参数值
        $assign_data['canshu'] = $assign_data['customParam'] = '';
        if (!empty($info['attrlist_id'])) {
            $assign_data['canshu'] = $this->ajax_get_shop_attr_input($typeid, $id, $info['attrlist_id']);
        }

        // 自定义参数
        $where = [
            'a.is_custom' => 1,
            'b.is_custom' => 1,
            'a.aid' => $info['aid'],
        ];
        $field = 'a.*, b.attr_name';
        $order = 'a.sort_order asc, b.attr_id sac, a.product_attr_id asc';
        $productAttr = Db::name('shop_product_attr')
            ->alias('a')
            ->where($where)
            ->field($field)
            ->order($order)
            ->join('__SHOP_PRODUCT_ATTRIBUTE__ b', 'a.attr_id = b.attr_id', 'LEFT')
            ->select();
        $assign_data['customParam'] = $productAttr;
        $delAttrID = get_arr_column($productAttr, 'attr_id');
        $assign_data['delAttrID'] = !empty($delAttrID) ? implode(',', $delAttrID) : '';

        // 最大参数属性ID值 +1
        $maxAttrID = Db::name('shop_product_attribute')->max('attr_id');
        $maxAttrID++;
        $assign_data['maxAttrID'] = $maxAttrID;

        // 文档属性
        $assign_data['archives_flags'] = model('ArchivesFlag')->getList();
        $channelRow = Db::name('channeltype')->where('id', $this->channeltype)->find();
        $assign_data['channelRow'] = $channelRow;

        // 来源列表
        // $system_originlist = tpSetting('system.system_originlist');
        // $system_originlist = json_decode($system_originlist, true);
        // $system_originlist = !empty($system_originlist) ? $system_originlist : [];
        // $assign_data['system_originlist_str'] = implode(PHP_EOL, $system_originlist);

        // 标签列表
        $where = [
            'status' => 1,
        ];
        $labelTypes = Db::name('label_types')->where($where)->order('sort_order asc, types_id desc')->select();
        $serviceLabel = $marketingLabel = [];
        foreach ($labelTypes as $key => $value) {
            if (1 === intval($value['types_value'])) {
                array_push($marketingLabel, $value);
            } else if (2 === intval($value['types_value'])) {
                array_push($serviceLabel, $value);
            }
        }
        $this->assign('serviceLabel', $serviceLabel);
        $this->assign('marketingLabel', $marketingLabel);

        // 视频类型
        $media_type = tpCache('basic.media_type');
        $media_type = !empty($media_type) ? $media_type : config('global.media_ext');
        $media_type = str_replace(",", "|", $media_type);
        $this->assign('media_type', $media_type);

        // 系统最大上传视频的大小
        $this->assign('upload_max_filesize', upload_max_filesize());

        // 商品标签
        $where = [
            'aid' => intval($info['aid'])
        ];
        $assign_data['product_label'] = Db::name('product_label')->where($where)->column('types_id');

        // 商城运费模板
        $where = [
            'status' => 1,
        ];
        $template = Db::name('shop_freight_template')->field('template_id, template_name, default')->where($where)->select();
        $this->assign('template', $template);

        $this->assign($assign_data);
        return $this->fetch();
    }

    // 获取运费模板列表更新
    public function ajaxGetTemplateList()
    {
        if (IS_AJAX_POST) {
            // 运费模板ID
            $freightFeeTpl = input('post.freight_fee_tpl/d', 0);
            // 商城运费模板
            $where = [
                'status' => 1,
            ];
            $template = Db::name('shop_freight_template')->field('template_id, template_name, default')->where($where)->select();
            $templateHtml = '<option value="0">请选择</option>';
            foreach ($template as $key => $value) {
                if (!empty($freightFeeTpl)) {
                    $selected = intval($value['template_id']) === intval($freightFeeTpl) ? 'selected' : '';
                } else {
                    $selected = !empty($value['default']) && 1 === intval($value['default']) ? 'selected' : '';
                }
                $templateHtml .= "<option value='{$value['template_id']}' {$selected}>{$value['template_name']}</option>";
            }
            $this->success('', null, $templateHtml);
        }
    }

    // 选择商品分类
    public function select_stypeid()
    {
        if (IS_AJAX_POST) {
            $stypeid = input('post.stypeid');
            $arctypehtml = '';
            if (!empty($stypeid)) {
                $stypeid = explode(',', $stypeid);
                $where = [
                    'a.id' => ['IN', $stypeid]
                ];
                $field = 'c.id as oneID, c.typename as oneName, b.id as twoID, b.typename as twoName, a.id as threeID, a.typename as threeName';
                $arctypeName = Db::name('arctype')->alias('a')
                    ->join('__ARCTYPE__ b', 'a.parent_id = b.id', 'LEFT')
                    ->join('__ARCTYPE__ c', 'a.topid = c.id', 'LEFT')
                    ->field($field)
                    ->where($where)
                    ->select();
                if (!empty($arctypeName)) {
                    foreach ($arctypeName as $key => $value) {
                        // 如果一级栏目和二级栏目相同则去除一级栏目名称
                        if (strval($value['oneID']) === strval($value['twoID'])) unset($value['oneName']);
                        // 拼装返回分类html
                        $arctypehtml .= '<div>';
                        $arctypehtml .= !empty($value['oneName']) ? strval($value['oneName']) . '->' : '';
                        $arctypehtml .= !empty($value['twoName']) ? strval($value['twoName']) . '->' : '';
                        $arctypehtml .= !empty($value['threeName']) ? strval($value['threeName']) : '';
                        $arctypehtml .= '<span data-id="' . $value['threeID'] . '" onclick="delStypeid(this);"> &nbsp; <a class="sort_style">删除</a></span>';
                        $arctypehtml .= '</div>';
                    }
                }
            }
            $this->success('查询成功', null, $arctypehtml);
        }

        // 目录列表
        $where = [
            'is_del' => 0,
            'current_channel' => 2
        ];
        $get_stypeid = input('param.get_stypeid/s', '');
        $this->assign('stypeidStr', $get_stypeid);
        $arctype_list = $this->arctypeLogic->arctype_list(0, 0, false, 0, $where, false);
        foreach ($arctype_list as $key => $value) {
            $value['litpic'] = get_default_pic($value['litpic']);
            $arctype_list[$key] = $value;
        }
        $this->assign('arctype_list', $arctype_list);

        // 获取所有有子栏目的栏目id
        $where = [
            'is_del' => 0,
            'parent_id' => ['gt', 0]
        ];
        $parent_ids = Db::name('arctype')->where($where)->group('parent_id')->cache(true, EYOUCMS_CACHE_TIME, 'arctype')->column('parent_id');
        $cookied_treeclicked =  json_decode(cookie('stypeid-treeClicked-Arr'));
        empty($cookied_treeclicked) && $cookied_treeclicked = [];
        $all_treeclicked = cookie('stypeid-treeClicked_All');
        empty($all_treeclicked) && $all_treeclicked = [];
        $tree = [
            'has_children'=>!empty($parent_ids) ? 1 : 0,
            'parent_ids'=>json_encode($parent_ids),
            'all_treeclicked'=>$all_treeclicked,
            'cookied_treeclicked'=>$cookied_treeclicked,
            'cookied_treeclicked_arr'=>json_encode($cookied_treeclicked),
        ];
        $this->assign('tree', $tree);

        return $this->fetch();
    }

    // 商品删除 -- 商品删除时，转用 product_delete 方法
    public function del()
    {
        if (IS_POST) {
            $archivesLogic = new \app\admin\logic\ArchivesLogic;
            $archivesLogic->del([], 0, 'product');
        }
    }

    // 删除商品相册图
    public function del_proimg()
    {
        if (IS_POST) {
            $aid = input('aid/d');
            $filename = input('filename/s');
            if (!empty($filename) && !empty($aid)) {
                Db::name('product_img')->where('image_url','like','%'.$filename)->where('aid',$aid)->delete();
            }

        }
    }

    // 商品状态更新
    public function product_status()
    {
        if (IS_AJAX_POST) {
            // 商品ID
            $aid = input('post.aid/a');
            $status = input('post.status/d', 0);
            if (empty($aid)) $this->error('请选择商品');
            // 执行商品状态更新
            $resultID = $this->productModel->productStatus($aid, $status);
            if (!empty($resultID)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败，刷新重试');
            }
        }
    }

    // 商品删除(query=1，2，3回收站、 query=4彻底删除)
    public function product_delete()
    {
        if (IS_AJAX_POST) {
            // 商品ID
            $aid = input('post.aid/a');
            $query = input('post.query/d', 0);
            if (empty($aid)) $this->error('请选择商品');
            // 执行商品删除
            $resultID = $this->productModel->productDelete($aid, $query);
            if (!empty($resultID)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败，刷新重试');
            }
        }
    }

    // 商品恢复(从伪删除恢复到原先状态)
    public function product_restore()
    {
        if (IS_AJAX_POST) {
            // 商品ID
            $aid = input('post.aid/a');
            if (empty($aid)) $this->error('请选择商品');
            // 执行商品恢复
            $resultID = $this->productModel->productRestore($aid);
            if (!empty($resultID)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败，刷新重试');
            }
        }
    }

    // 商品复制
    public function product_copy()
    {
        if (IS_AJAX_POST) {
            // 商品ID
            $aid = input('post.aid/a');
            if (empty($aid)) $this->error('请选择商品');
            // 执行商品复制
            $resultID = $this->productModel->productCopy($aid);
            if (!empty($resultID)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败，刷新重试');
            }
        }
    }

    // 添加商品自定义规格并返回规格表格
    public function ajaxAddCustomSpec()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 添加自定义规格
            $resultArray = $this->productSpecLogic->addProductCustomSpec($post);
            if (!empty($resultArray['errorMsg'])) $this->error($resultArray['errorMsg']);

            // 获取已选规格进行HTML代码拼装
            if (!empty($post['aid'])) {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray['spec_array'], $post['aid']);
            } else {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray['spec_array']);
            }
            // 返回数据
            if (in_array($post['action'], ['specName', 'specValue'])) {
                $specData = $this->productSpecLogic->getProductSpecValueOption($resultArray['spec_mark_id'], $post);
            }
            $returnData = [
                'htmlTable' => !empty($htmlTable) ? $htmlTable : ' ',
                'spec_name' => !empty($specData['spec_name']) ? $specData['spec_name'] : '',
                'spec_value' => !empty($specData['spec_value']) ? $specData['spec_value'] : '',
                'spec_mark_id' => !empty($resultArray['spec_mark_id']) ? $resultArray['spec_mark_id'] : 0,
                'spec_value_id' => !empty($resultArray['spec_value_id']) ? $resultArray['spec_value_id'] : 0,
                'spec_value_option' => !empty($specData['spec_value_option']) ? $specData['spec_value_option'] : '',
                'spec_mark_id_arr' => !empty($resultArray['spec_mark_id_arr']) ? $resultArray['spec_mark_id_arr'] : 0,
                'preset_name_option' => !empty($specData['preset_name_option']) ? $specData['preset_name_option'] : '',
            ];
            
            $this->success('加载成功！', null, $returnData);
        }
    }

    // 删除商品自定义规格并返回规格表格
    public function ajaxDelCustomSpec()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 删除自定义规格
            $resultArray = $this->productSpecLogic->delProductCustomSpec($post);
            // 获取已选规格进行HTML代码拼装
            if (!empty($post['aid'])) {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray, $post['aid']);
            } else {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray);
            }
            if (in_array($post['del'], ['specName', 'specValue'])) {
                $specData = $this->productSpecLogic->getProductSpecValueOption(0, $post);
            }
            // 返回数据
            $returnData = [
                'htmlTable' => !empty($htmlTable) ? $htmlTable : ' ',
                'spec_value_option' => !empty($specData['spec_value_option']) ? $specData['spec_value_option'] : '',
                'preset_name_option' => !empty($specData['preset_name_option']) ? $specData['preset_name_option'] : '',
            ];
            $this->success('加载成功！', null, $returnData);
        }
    }

    // 添加自定义规格名称并返回规格表
    public function ajaxSetCustomSpecName()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 添加自定义规格名称
            $resultArray = $this->productSpecLogic->addProductCustomSpecName($post);
            if (!empty($resultArray['errorMsg'])) $this->error($resultArray['errorMsg']);

            // 获取已选规格进行HTML代码拼装
            if (!empty($post['aid'])) {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray, $post['aid']);
            } else {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray);
            }
            // 返回数据
            $returnData = [
                'htmlTable' => !empty($htmlTable) ? $htmlTable : ' ',
            ];
            $this->success('加载成功！', null, $returnData);
        }
    }

    // 添加自定义规格值并返回规格表
    public function ajaxSetCustomSpecValue()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 添加自定义规格值
            $resultArray = $this->productSpecLogic->addProductCustomSpecValue($post);
            if (!empty($resultArray['errorMsg'])) $this->error($resultArray['errorMsg']);

            // 获取已选规格进行HTML代码拼装
            if (!empty($post['aid'])) {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray, $post['aid']);
            } else {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray);
            }
            // 返回数据
            $returnData = [
                'htmlTable' => !empty($htmlTable) ? $htmlTable : ' ',
            ];
            $this->success('加载成功！', null, $returnData);
        }
    }

    // 标签列表
    public function label_types()
    {
        // 标签类型
        $types_value = input('param.types_value/d', 0);

        // 强制删除空的商品服务
        $where = [
            'types_names' => ['EQ', ''],
            'types_intro' => ['EQ', ''],
            'types_value' => intval($types_value),
        ];
        Db::name('label_types')->where($where)->delete(true);

        // 查询商品服务
        $where = [
            'status' => 1,
            'types_value' => intval($types_value),
        ];
        $count = Db::name('label_types')->where($where)->count('types_id');
        $pageObj = new Page($count, config('paginate.list_rows'));
        $list = Db::name('label_types')->where($where)->order('sort_order asc,types_id asc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();

        $this->assign('list', $list);
        $this->assign('pager', $pageObj);
        $this->assign('page', $pageObj->show());

        if (1 === intval($types_value)) {
            return $this->fetch('product_label');
        } else if (2 === intval($types_value)) {
            return $this->fetch('product_service');
        } else {
            return $this->fetch();
        }
    }

    // 商品服务新增
    public function ajaxLabelTypesAdd()
    {
        if (IS_AJAX_POST) {
            $times = getTime();
            $types_value = input('post.types_value/d', 0);
            $types_id = Db::name('label_types')->where(['types_value'=>$types_value])->max('types_id');
            $types_id = intval($types_id);
            $insert = [
                'types_id' => $types_id + 1,
                'types_value' => intval($types_value),
                'types_names' => '',
                'types_intro' => '',
                'sort_order' => 100,
                'status' => 0,
                'add_time' => $times,
                'update_time' => $times,
            ];
            $insertID = Db::name('label_types')->insertGetId($insert);
            if (!empty($insertID)) {
                $insert['types_id'] = $insertID;
                $this->success('新增成功', null, $insert);
            } else {
                $this->error('新增失败，刷新重试');
            }
        }
    }

    // 商品服务删除
    public function ajaxLabelTypesDel()
    {
        if (IS_AJAX_POST) {
            $types_id = input('post.types_id/d', 0);
            $types_value = input('post.types_value/d', 0);
            if (empty($types_id) || empty($types_value)) $this->error('请选择删除标签');
            $where = [
                'types_id' => intval($types_id),
                'types_value' => intval($types_value),
            ];
            $deleteID = Db::name('label_types')->where($where)->delete(true);
            if (!empty($deleteID)) {
                // 同步删除商品标签关联表的指定标签
                $where = [
                    'types_id' => intval($types_id),
                ];
                Db::name('product_label')->where($where)->delete(true);
                
                $this->success('删除成功');
            } else {
                $this->error('删除失败，刷新重试');
            }
        }
    }

    // 商品分类列表
    public function arctype_index()
    {
        // 目录列表
        $where = [
            'is_del' => 0,
            'current_channel' => 2
        ];
        $arctype_list = $this->arctypeLogic->arctype_list(0, 0, false, 0, $where, false);
        $this->assign('arctype_list', $arctype_list);

        // 模型列表
        $channeltype_list = getChanneltypeList();
        $this->assign('channeltype_list', $channeltype_list);

        // 栏目最多级别
        $arctype_max_level = intval(config('global.arctype_max_level'));
        $this->assign('arctype_max_level', $arctype_max_level);
        
        // 生成静态页面代码
        $typeid = input('param.typeid/d',0);
        $is_del = input('param.is_del/d',0);
        $handle = input('param.handle/s','');
        $this->assign('typeid',$typeid);
        $this->assign('is_del',$is_del);
        $typeurl = '';
        $seo_pseudo = tpCache('global.seo_pseudo');//URL模式
        $sitemap_zzbaidutoken = config('tpcache.sitemap_zzbaidutoken');//实时推送Url的token
        $bdminipro = Db::name('weapp')->where(['code'=>'BdDiyminipro','status'=>1])->find();
        if (!empty($typeid)){
            $typeData = Db::name('arctype')->where('id',$typeid)->find();
            if (!empty($typeData)) $typeurl = get_typeurl($typeData);
        }
        $eyou = [
            'handle' => $handle,
            'typeurl' => $typeurl,
            'bdminipro' => $bdminipro,
            'seo_pseudo' => $seo_pseudo,
            'zzbaidutoken' => $sitemap_zzbaidutoken,
        ];
        $this->assign('eyou',$eyou);

        // 回收站开关
        $recycle_switch = tpSetting('recycle.recycle_switch');
        $this->assign('recycle_switch', $recycle_switch);

        // 获取所有有子栏目的栏目id
        $where = [
            'is_del' => 0,
            'parent_id' => ['gt', 0]
        ];
        $parent_ids = Db::name('arctype')->where($where)->group('parent_id')->cache(true, EYOUCMS_CACHE_TIME, 'arctype')->column('parent_id');
        $cookied_treeclicked =  json_decode(cookie('admin-treeClicked-Arr'));
        empty($cookied_treeclicked) && $cookied_treeclicked = [];
        $all_treeclicked = cookie('admin-treeClicked_All');
        empty($all_treeclicked) && $all_treeclicked = [];
        $tree = [
            'has_children'=>!empty($parent_ids) ? 1 : 0,
            'parent_ids'=>json_encode($parent_ids),
            'all_treeclicked'=>$all_treeclicked,
            'cookied_treeclicked'=>$cookied_treeclicked,
            'cookied_treeclicked_arr'=>json_encode($cookied_treeclicked),
        ];
        $this->assign('tree', $tree);

        return $this->fetch();
    }

    // 商品分类添加
    public function arctype_add()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            if (empty($post['typename'])) $this->error('请输入分类名称');

            $times = getTime();
            // 分类名称处理
            $typename = addslashes(htmlspecialchars(strip_tags(strip_sql($post['typename']))));
            // 分类纯拼音处理
            $dirname = $this->arctypeLogic->get_dirname($post['typename']);
            // 分类路径处理
            $dirpath = !empty($post['dirpath']) ? rtrim($post['dirpath'], '/') : '';
            // 新增分类数据
            $insert = [
                'channeltype' => $this->channeltype,
                'current_channel' => $this->channeltype,
                'topid' => !empty($post['topid']) ? intval($post['topid']) : 0,
                'parent_id' => !empty($post['parent_id']) ? intval($post['parent_id']) : intval($post['topid']),
                'grade' => 0,
                'typename' => $typename,
                'dirname' => $dirname,
                'dirpath' => $dirpath . '/' . $dirname,
                'diy_dirpath' => $dirpath . '/' . $dirname,
                'litpic' => !empty($post['litpic']) ? get_default_pic($post['litpic']) : '',
                'ad_image' => !empty($post['ad_image']) ? get_default_pic($post['ad_image']) : '',
                'ad_host_id' => !empty($post['ad_host_id']) ? intval($post['ad_host_id']) : 0,
                'ad_link_id' => !empty($post['ad_link_id']) ? intval($post['ad_link_id']) : 0,
                'is_hidden' => !empty($post['is_hidden']) ? intval($post['is_hidden']) : 0,
                'templist' => 'lists_product.htm',
                'tempview' => 'view_product.htm',
                'sort_order' => 100,
                'add_time' => $times,
                'update_time' => $times,
            ];
            if (!empty($post['topid'])) $insert['grade'] = 1;
            if (!empty($post['parent_id'])) $insert['grade'] = 2;

            $insertID = Db::name('arctype')->insertGetId($insert);
            if (!empty($insertID)) {
                $this->success('新增成功', url('admin/ShopProduct/arctype_index'));
            } else {
                $this->error('新增失败');
            }
        }

        // 上级分类ID
        $parent_id = input('param.parent_id/d', 0);
        // 商品一级分类
        $where = [
            'is_del' => 0,
            'parent_id' => 0,
            'current_channel' => $this->channeltype
        ];
        $arctype_list = model('Arctype')->getAll('id, typename', $where, 'id');
        // 新增栏目在指定的上一级栏目下
        $topid = 0;
        $grade = 0;
        $parent_show = 0;
        $arctype_list2 = [];
        if (0 < intval($parent_id)) {
            $info = Db::name('arctype')->field('parent_id, grade, topid')->find($parent_id);
            if (!empty($info)) {
                $grade = $info['grade'] + 1;
                $topid = !empty($info['topid']) ? $info['topid'] : $parent_id;
            }
            // 二级分类
            if (2 <= intval($grade)) {
                $where = array(
                    'is_del' => 0,
                    'parent_id' => $info['parent_id'],
                    'current_channel' => $this->channeltype
                );
                $parent_show = 1;
                $arctype_list2 = model('Arctype')->getAll('id, typename', $where, 'id');
            }
        }
        $this->assign('topid', $topid);
        $this->assign('grade', $grade);
        $this->assign('parent_id', $parent_id);
        $this->assign('parent_show', $parent_show);
        $this->assign('arctype_list', $arctype_list);
        $this->assign('arctype_list2', $arctype_list2);

        return $this->fetch();
    }

    // 商品分类添加
    public function arctype_edit()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            if (empty($post['typename'])) $this->error('请输入分类名称');

            $times = getTime();
            // 分类名称处理
            $typename = addslashes(htmlspecialchars(strip_tags(strip_sql($post['typename']))));
            // 分类纯拼音处理
            $dirname = $this->arctypeLogic->get_dirname($post['typename'], $post['dirname'], $post['id']);
            // 分类路径处理
            $dirpath = !empty($post['dirpath']) ? rtrim($post['dirpath'], '/') : '';
            // 新增分类数据
            $update = [
                'topid' => !empty($post['topid']) ? intval($post['topid']) : 0,
                'parent_id' => !empty($post['parent_id']) ? intval($post['parent_id']) : intval($post['topid']),
                'grade' => !empty($post['grade']) ? intval($post['grade']) : 0,
                'typename' => $typename,
                'dirname' => $dirname,
                'dirpath' => $dirpath . '/' . $dirname,
                'diy_dirpath' => $dirpath . '/' . $dirname,
                'litpic' => !empty($post['litpic']) ? get_default_pic($post['litpic']) : '',
                'ad_image' => !empty($post['ad_image']) ? get_default_pic($post['ad_image']) : '',
                'ad_host_id' => !empty($post['ad_host_id']) ? intval($post['ad_host_id']) : 0,
                'ad_link_id' => !empty($post['ad_link_id']) ? intval($post['ad_link_id']) : 0,
                'is_hidden' => !empty($post['is_hidden']) ? intval($post['is_hidden']) : 0,
                'update_time' => $times,
            ];
            if (!empty($post['topid'])) $update['grade'] = 1;
            if (!empty($post['parent_id'])) $update['grade'] = 2;

            $updateID = Db::name('arctype')->where('id', intval($post['id']))->update($update);
            if (!empty($updateID)) {
                $this->success('编辑成功', url('admin/ShopProduct/arctype_index'));
            } else {
                $this->error('编辑失败');
            }
        }

        // 分类ID
        $id = input('param.id/d', 0);
        if (empty($id)) $this->error('请选择分类');
        $where = [
            'a.id' => $id,
            'a.status' => 1,
            'a.is_del' => 0,
            'a.is_hidden' => 0,
        ];
        $field = 'a.*, b.link_names, b.host_id';
        $arctype = Db::name('arctype')->alias('a')->join('__AD_LINK__ b', 'a.ad_link_id = b.link_id', 'LEFT')->field($field)->where($where)->find();
        $data = get_ad_link_url([$arctype]);
        $arctype = current($data);

        // 上级分类ID
        $parent_id = !empty($arctype['parent_id']) ? intval($arctype['parent_id']) : 0;
        // 商品一级分类
        $where = [
            'is_del' => 0,
            'parent_id' => 0,
            'current_channel' => $this->channeltype
        ];
        $arctype_list = model('Arctype')->getAll('id, typename', $where, 'id');
        // 新增栏目在指定的上一级栏目下
        $grade = !empty($arctype['grade']) ? $arctype['grade']++ : 0;
        $topid = !empty($arctype['topid']) ? intval($arctype['topid']) : 0;
        $parent_show = 0;
        $arctype_list2 = [];
        if (0 < intval($parent_id) && 2 <= intval($grade)) {
            // 二级分类
            $where = array(
                'is_del' => 0,
                'parent_id' => $parent_id,
                'current_channel' => $this->channeltype
            );
            $parent_show = 1;
            $arctype_list2 = model('Arctype')->getAll('id, typename', $where, 'id');
        }

        $this->assign('topid', $topid);
        $this->assign('grade', $grade);
        $this->assign('arctype', $arctype);
        $this->assign('parent_id', $parent_id);
        $this->assign('parent_show', $parent_show);
        $this->assign('arctype_list', $arctype_list);
        $this->assign('arctype_list2', $arctype_list2);

        return $this->fetch();
    }

    // 商品分类删除
    public function arctype_del()
    {
        if (IS_AJAX_POST) {
            $id = input('id/d', 0);
            if (empty($id)) $this->error('请选择要删除的分类');

            // 查询分类级别
            $where = [
                'id' => $id,
                'current_channel' => $this->channeltype
            ];
            $grade = Db::name('arctype')->where($where)->getField('grade');
            // 查询要删除的分类ID组
            if (0 === intval($grade)) {
                $where = [
                    'id|topid' => $id,
                    'current_channel' => $this->channeltype
                ];
                $columnID = Db::name('arctype')->where($where)->column('id');
            } else if (1 === intval($grade)) {
                $where = [
                    'id|parent_id' => $id,
                    'current_channel' => $this->channeltype
                ];
                $columnID = Db::name('arctype')->where($where)->column('id');
            } else if (2 === intval($grade)) {
                $columnID = [$id];
            }

            // 执行删除指定分类
            $where = [
                'id' => ['IN', $columnID],
                'current_channel' => $this->channeltype
            ];
            $deleteID = Db::name('arctype')->where($where)->delete(true);
            // 删除分类后续操作
            if (!empty($deleteID)) {
                // 更新分类所属商品为无分类
                $where = [
                    'typeid' => ['IN', $columnID],
                    'channel' => $this->channeltype
                ];
                $update = [
                    'typeid' => 0,
                    'update_time' => getTime()
                ];
                Db::name('archives')->where($where)->update($update);
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }
    }

    // 商品分类设置
    public function arctype_conf()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            $r = getUsersConfigData('shop', $post['shop']);
            if ($r !== false) {
                $this->success('操作成功', url('ShopProduct/arctype_conf'));
            } else {
                $this->error('操作失败');
            }
        }
    }

    // 获取指定分类的下级分类
    public function ajaxGetUnderType()
    {
        if (IS_AJAX_POST) {
            $id = input('post.id/d');
            $parent_id = input('post.parent_id/d');
            if (!empty($parent_id)) {
                $arctype = Db::name('arctype')->field('id, current_channel, typename')->where('parent_id', $parent_id)->select();
                $selecthtml = '';
                if (!empty($arctype)) {
                    if (empty($id) && 0 == $id) {
                        $selecthtml .= "<option value='0' selected='true'>选择分类</option>";
                    } else {
                        $selecthtml .= "<option value='0'>选择分类</option>";
                    }
                    foreach ($arctype as $key => $value) {
                        if ($value['id'] == $id) {
                            $selecthtml .= "<option value='{$value['id']}' selected='true'>{$value['typename']}</option>";
                        } else {
                            $selecthtml .= "<option value='{$value['id']}'>{$value['typename']}</option>";
                        }
                    }
                }
                $data = [
                    'selecthtml' => $selecthtml,
                ];
                $this->success('请求成功', null, $data);
            }
        }
        $this->error('请求失败');
    }

    // 分类列表图片上传
    public function ajaxArctypeLitpicUpload()
    {
        if (IS_AJAX_POST) {
            // 传入参数
            $post = input('post.');
            // 存在分类ID和分类图片则执行
            if (!empty($post['id']) && !empty($post['litpic'])) {
                $update = [
                    'id' => intval($post['id']),
                    'litpic' => strval($post['litpic']),
                    'update_time' => getTime(),
                ];
                $updateID = Db::name('arctype')->update($update);
                if (!empty($updateID)) {
                    $this->success('操作成功');
                } else {
                    $this->error('操作失败，刷新重试');
                }
            }
        }
    }

    // 添加商品自定义规格并返回规格表格
    public function add_product_custom_spec()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 添加自定义规格
            $resultArray = $this->productSpecLogic->addProductCustomSpec($post);
            if (!empty($resultArray['errorMsg'])) $this->error($resultArray['errorMsg']);

            // 获取已选规格进行HTML代码拼装
            if (!empty($post['aid'])) {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray['spec_array'], $post['aid']);
            } else {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray['spec_array']);
            }
            // 返回数据
            if (in_array($post['action'], ['specName', 'specValue'])) {
                $specData = $this->productSpecLogic->getProductSpecValueOption($resultArray['spec_mark_id'], $post);
            }
            $returnData = [
                'htmlTable' => !empty($htmlTable) ? $htmlTable : ' ',
                'spec_name' => !empty($specData['spec_name']) ? $specData['spec_name'] : '',
                'spec_value' => !empty($specData['spec_value']) ? $specData['spec_value'] : '',
                'spec_mark_id' => !empty($resultArray['spec_mark_id']) ? $resultArray['spec_mark_id'] : 0,
                'spec_value_id' => !empty($resultArray['spec_value_id']) ? $resultArray['spec_value_id'] : 0,
                'spec_value_option' => !empty($specData['spec_value_option']) ? $specData['spec_value_option'] : '',
                'spec_mark_id_arr' => !empty($resultArray['spec_mark_id_arr']) ? $resultArray['spec_mark_id_arr'] : 0,
                'preset_name_option' => !empty($specData['preset_name_option']) ? $specData['preset_name_option'] : '',
            ];
            
            $this->success('加载成功！', null, $returnData);
        }
    }

    // 添加自定义规格名称并返回规格表
    public function add_product_custom_spec_name()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 添加自定义规格名称
            $resultArray = $this->productSpecLogic->addProductCustomSpecName($post);
            if (!empty($resultArray['errorMsg'])) $this->error($resultArray['errorMsg']);

            // 获取已选规格进行HTML代码拼装
            if (!empty($post['aid'])) {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray, $post['aid']);
            } else {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray);
            }
            // 返回数据
            $returnData = [
                'htmlTable' => !empty($htmlTable) ? $htmlTable : ' ',
            ];
            $this->success('加载成功！', null, $returnData);
        }
    }

    // 添加自定义规格值并返回规格表
    public function add_product_custom_spec_value()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 添加自定义规格值
            $resultArray = $this->productSpecLogic->addProductCustomSpecValue($post);
            if (!empty($resultArray['errorMsg'])) $this->error($resultArray['errorMsg']);

            // 获取已选规格进行HTML代码拼装
            if (!empty($post['aid'])) {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray, $post['aid']);
            } else {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray);
            }
            // 返回数据
            $returnData = [
                'htmlTable' => !empty($htmlTable) ? $htmlTable : ' ',
            ];
            $this->success('加载成功！', null, $returnData);
        }
    }

    // 删除商品自定义规格并返回规格表格
    public function del_product_custom_spec()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 删除自定义规格
            $resultArray = $this->productSpecLogic->delProductCustomSpec($post);
            // 获取已选规格进行HTML代码拼装
            if (!empty($post['aid'])) {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray, $post['aid']);
            } else {
                $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($resultArray);
            }
            if (in_array($post['del'], ['specName', 'specValue'])) {
                $specData = $this->productSpecLogic->getProductSpecValueOption(0, $post);
            }
            // 返回数据
            $returnData = [
                'htmlTable' => !empty($htmlTable) ? $htmlTable : ' ',
                'spec_value_option' => !empty($specData['spec_value_option']) ? $specData['spec_value_option'] : '',
                'preset_name_option' => !empty($specData['preset_name_option']) ? $specData['preset_name_option'] : '',
            ];
            $this->success('加载成功！', null, $returnData);
        }
    }

    // 选中规格名称值，追加html到页面展示
    public function spec_value_select()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            // Post 数据
            $aid = !empty($post['aid']) ? $post['aid'] : 0;
            $spec_mark_id = !empty($post['spec_mark_id']) ? $post['spec_mark_id'] : 0;
            $spec_value_id = !empty($post['spec_value_id']) ? $post['spec_value_id'] : 0;
            if (empty($aid) || empty($spec_mark_id) || empty($spec_value_id)) $this->error('操作异常，请刷新重试...');

            $spec_array = [];
            // 执行更新
            $where = [
                'aid' => $aid,
                'spec_mark_id' => $spec_mark_id,
                'spec_value_id' => $spec_value_id,
            ];
            $update = [
                'spec_is_select' => 1,
                'update_time' => getTime()
            ];
            $Value = Db::name('product_spec_data_handle')->where($where)->getField('spec_value');
            $isResult = Db::name('product_spec_data_handle')->where($where)->update($update);
            if (!empty($isResult)) {
                // 仅ID信息，二维数组形式
                $where = [
                    'aid' => $aid,
                    'spec_is_select' => 1,
                ];
                $order = 'spec_value_id asc, spec_id asc, spec_mark_id asc';
                $data = Db::name('product_spec_data_handle')->field('spec_mark_id, spec_value_id')->where($where)->order($order)->select();
                if (!empty($data)) {
                    foreach ($data as $key => $value) {
                        $spec_array[$value['spec_mark_id']][] = $value['spec_value_id'];
                    }
                }
            }

            // 剔除已选择的规格值查询未选择的规格值组装成下拉返回
            $notInPresetID = !empty($spec_array[$specMarkID]) ? $spec_array[$specMarkID] : [];
            $where = [
                'aid' => $aid,
                'spec_is_select' => 0,
                'spec_mark_id' => ['IN', $spec_mark_id],
            ];
            if (!empty($notInPresetID)) $where['spec_value_id'] = ['NOT IN', $notInPresetID];
            $specData = Db::name('product_spec_data_handle')->where($where)->order('spec_value_id asc')->select();

            // 拼装下拉选项
            $Option = '<option value="0">选择规格值</option>';
            if (!empty($specData)) {
                foreach ($specData as $value) {
                    $Option .= "<option value='{$value['spec_value_id']}'>{$value['spec_value']}</option>";
                }
            }

            $htmlTable = $this->productSpecLogic->SpecAssemblyEdit($spec_array, $aid);

            // 返回数据
            $returnHtml = [
                'Value' => $Value,
                'Option' => $Option,
                'htmlTable' => $htmlTable
            ];
            $this->success('加载成功！', null, $returnHtml);
        }
    }

    // 商品属性列表
    public function attrlist_index()
    {
        // 查询条件
        $Where = [];
        $keywords        = input('keywords/s');
        if (!empty($keywords)) $Where['list_name'] = ['LIKE', "%{$keywords}%"];
        $Where['is_del'] = 0;

        // 分页
        $count   = $this->shop_product_attrlist_db->where($Where)->count('list_id');
        $pageObj = new Page($count, config('paginate.list_rows'));
        $pageStr = $pageObj->show();
        $this->assign('pager', $pageObj);
        $this->assign('page', $pageStr);

        // 数据
        $list = $this->shop_product_attrlist_db
            ->where($Where)
            ->order('sort_order asc, list_id asc')
            ->limit($pageObj->firstRow . ',' . $pageObj->listRows)
            ->select();
        $this->assign('list', $list);

        // 内容管理的产品发布/编辑里入口进来
        $oldinlet = input('param.oldinlet/d');
        $this->assign('oldinlet', $oldinlet);

        return $this->fetch();
    }

    // 保存全部参数
    public function attrlist_save()
    {
        function_exists('set_time_limit') && set_time_limit(0);

        if (IS_AJAX_POST) {
            $post = input('post.');
            // 参数名称不可重复
            $ListName = array_unique($post['list_name']);
            if (count($ListName) != count($post['list_name'])) $this->error('参数名称不可重复！');

            // 数据拼装
            $SaveData = [];
            foreach ($ListName as $key => $value) {
                if (!empty($value)) {
                    $list_id   = $post['list_id'][$key];
                    $list_name = trim($value);

                    $SaveData[$key] = [
                        'list_id'     => !empty($list_id) ? $list_id : 0,
                        'list_name'   => $list_name,
                        'desc'        => !empty($post['desc'][$key]) ? $post['desc'][$key] : '',
                        'sort_order'  => !empty($post['sort_order'][$key]) ? $post['sort_order'][$key] : 100,
                        'update_time' => getTime()
                    ];

                    if (empty($list_id)) {
                        $SaveData[$key]['add_time'] = getTime();
                        unset($SaveData[$key]['list_id']);
                    }
                }
            }

            $ReturnId = model('ShopProductAttrlist')->saveAll($SaveData);
            if ($ReturnId) {
                adminLog('新增商品参数：' . implode(',', $post['list_name']));
                $this->success('操作成功', url('Product/attrlist_index'));
            } else {
                $this->error('操作失败');
            }
        }
    }

    /**
     * 新增参数
     * @return [type] [description]
     */
    public function attrlist_add()
    {
        if (IS_AJAX_POST) {
            $post              = input('post.');
            $post['list_name'] = trim($post['list_name']);
            if (empty($post['list_name'])) {
                $this->error('参数名称不能为空！');
            }

            $SaveData = [
                'list_name'   => $post['list_name'],
                'desc'        => trim($post['desc']),
                'sort_order'  => 100,
                'add_time'    => getTime(),
                'update_time' => getTime(),
            ];

            $ReturnId = Db::name('shop_product_attrlist')->insertGetId($SaveData);
            if ($ReturnId) {
                adminLog('新增商品参数：' . $post['list_name']);
                if (!empty($post['attr_name'])) {
                    //数据拼接
                    $saveAttrData = [];
                    foreach ($post['attr_name'] as $k => $v) {
                        $attr_values           = str_replace('_', '', $v); // 替换特殊字符
                        $attr_values           = str_replace('@', '', $attr_values); // 替换特殊字符
                        $attr_values           = trim($attr_values);
                        if (empty($attr_values)) {
                            unset($post['attr_name'][$k]);
                            continue;
                        }
                        $post['attr_name'][$k] = $attr_values;

                        $saveAttrData[] = array(
                            'attr_name'       => !empty($post['attr_name'][$k]) ? $post['attr_name'][$k] : '',
                            'list_id'         => $ReturnId,
                            'attr_input_type' => !empty($post['attr_input_type'][$k]) ? intval($post['attr_input_type'][$k]) : 0,
                            'attr_values'     => !empty($post['attr_values'][$k]) ? trim($post['attr_values'][$k]) : '',
                            'sort_order'      => isset($post['attr_sort_order'][$k]) ? intval($post['attr_sort_order'][$k]) : 100,
                            'status'          => 1,
                            'add_time'        => getTime(),
                            'update_time'     => getTime(),
                        );
                    }

                    if (!empty($saveAttrData)) {
                        $rdata = model('ShopProductAttribute')->saveAll($saveAttrData);
                        if ($rdata !== false) {
                            // 参数值合计增加
                            Db::name('shop_product_attrlist')->where('list_id', $ReturnId)->setInc('attr_count', count($post['attr_name']));
                        }
                    }
                }
                $this->success('操作成功', url('ShopProduct/attrlist_index'));
            }
            $this->error('操作失败');
        }
        return $this->fetch();
    }

    /**
     * 编辑参数
     * @return [type] [description]
     */
    public function attrlist_edit()
    {
        if (IS_AJAX_POST) {
            $post              = input('post.');
            $post['list_id'] = intval($post['list_id']);
            $post['list_name'] = trim($post['list_name']);
            if (empty($post['list_name'])) {
                $this->error('参数名称不能为空！');
            }

            $SaveData = [
                'list_name'   => $post['list_name'],
                'desc'        => trim($post['desc']),
                'update_time' => getTime(),
            ];

            $res = Db::name('shop_product_attrlist')->where('list_id', $post['list_id'])->update($SaveData);
            if ($res) {
                adminLog('编辑商品参数：' . $post['list_name']);
                if (!empty($post['attr_name'])) {
                    //数据拼接
                    $saveAttrData = [];
                    $attr_ids     = [];
                    $time = getTime();

                    foreach ($post['attr_name'] as $k => $v) {
                        $attr_values           = str_replace('_', '', $v); // 替换特殊字符
                        $attr_values           = str_replace('@', '', $attr_values); // 替换特殊字符
                        $post['attr_name'][$k] = trim($attr_values);

                        $attrData = array(
                            'attr_name'       => !empty($post['attr_name'][$k]) ? $post['attr_name'][$k] : '',
                            'list_id'         => !empty($post['list_id']) ? intval($post['list_id']) : 0,
                            'attr_input_type' => !empty($post['attr_input_type'][$k]) ? intval($post['attr_input_type'][$k]) : 0,
                            'attr_values'     => !empty($post['attr_values'][$k]) ? trim($post['attr_values'][$k]) : '',
                            'sort_order'      => isset($post['attr_sort_order'][$k]) ? intval($post['attr_sort_order'][$k]) : 100,
                            'update_time'     => $time,
                        );
                        if (!empty($post['attr_id'][$k])) {
                            $attrData['attr_id'] = $post['attr_id'][$k];
                            $attrData['add_time'] = $time;
                            $attr_ids[]          = $post['attr_id'][$k];
                        }
                        $saveAttrData[] = $attrData;
                    }

                    if (!empty($saveAttrData)) {
                        $RId = model('ShopProductAttribute')->saveAll($saveAttrData);
                        if ($RId !== false) {
                            //删除多余的参数
                            Db::name('shop_product_attribute')
                                ->where([
                                    'list_id'   => $post['list_id'],
                                    'attr_id'   => ['NOTIN', $attr_ids],
                                    'update_time'=> ['NEQ', $time],
                                ])
                                ->delete();
                            // 参数值合计增加
                            Db::name('shop_product_attrlist')->where('list_id', $post['list_id'])->update(['attr_count' => count($saveAttrData), 'update_time' => getTime()]);
                        } else {
                            $this->error('操作失败');
                        }
                    }
                }else{
                    //删除多余的参数
                    Db::name('shop_product_attribute')
                        ->where('list_id', $post['list_id'])
                        ->delete();
                    // 参数值合计增加
                    Db::name('shop_product_attrlist')->where('list_id', $post['list_id'])->update(['attr_count' => 0, 'update_time' => getTime()]);
                }
                $this->success('操作成功', url('ShopProduct/attrlist_index'));
            }
            $this->error('操作失败');
        }
        
        $list_id = input('param.list_id');
        $list    = Db::name('shop_product_attrlist')->where('list_id', $list_id)->find();
        if (empty($list)) $this->error('数据不存在，请联系管理员！');
        $list['attr'] = Db::name('shop_product_attribute')->where('list_id', $list_id)->order('sort_order asc, attr_id asc')->select();

        $this->assign('list', $list);
        return $this->fetch();
    }

    // 参数删除
    public function attrlist_del()
    {
        $id_arr = input('del_id/a');
        $id_arr = eyIntval($id_arr);
        if (!empty($id_arr)) {
            $Result = $this->shop_product_attrlist_db->where('list_id', 'IN', $id_arr)->delete();
            if ($Result) {
                Db::name('shop_product_attribute')->where('list_id', 'IN', $id_arr)->delete();
                adminLog('删除商品参数-id：' . implode(',', $id_arr));
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('参数有误');
        }
    }

    /**
     * 商品参数值列表
     */
    public function attribute_index()
    {
        $condition = array();
        // 获取到所有GET参数
        $get     = input('get.');
        $list_id = input('list_id/d', 0);

        // 应用搜索条件
        foreach (['keywords', 'list_id'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                if ($key == 'keywords') {
                    $condition['a.attr_name'] = ['LIKE', "%{$get[$key]}%"];
                } else if ($key == 'list_id') {
                    $condition['a.list_id'] = $list_id;
                } else {
                    $condition['a.' . $key] = ['eq', $get[$key]];
                }
            }
        }
        $condition['a.is_del'] = 0;

        // 分页
        $count   = Db::name('shop_product_attribute')->alias('a')->where($condition)->count();
        $pageObj = new Page($count, config('paginate.list_rows'));
        $pageStr = $pageObj->show();
        $this->assign('pager', $pageObj);
        $this->assign('page', $pageStr);

        // 数据
        $list = Db::name('shop_product_attribute')
            ->alias('a')
            ->where($condition)
            ->order('a.sort_order asc, a.attr_id asc')
            ->limit($pageObj->firstRow . ',' . $pageObj->listRows)
            ->select();

        $attrInputTypeArr = [
            0 => '手工录入',
            1 => '选取默认值'
        ];
        $this->assign('attrInputTypeArr', $attrInputTypeArr);
        $this->assign('list', $list);
        return $this->fetch();
    }


    /**
     * 新增商品参数
     */
    public function attribute_add()
    {
        //防止php超时
        function_exists('set_time_limit') && set_time_limit(0);

        if (IS_AJAX_POST) {
            $attr_values              = str_replace('_', '', input('attr_values')); // 替换特殊字符
            $attr_values              = str_replace('@', '', $attr_values); // 替换特殊字符
            $attr_values              = trim($attr_values);
            $post_data                = input('post.');
            $post_data['list_id'] = intval($post_data['list_id']);
            $post_data['attr_values'] = $attr_values;

            $SaveData = array(
                'attr_name'       => $post_data['attr_name'],
                'list_id'         => $post_data['list_id'],
                'attr_input_type' => isset($post_data['attr_input_type']) ? $post_data['attr_input_type'] : '',
                'attr_values'     => isset($post_data['attr_values']) ? $post_data['attr_values'] : '',
                'sort_order'      => $post_data['sort_order'],
                'status'          => 1,
                'add_time'        => getTime(),
                'update_time'     => getTime(),
            );

            $ReturnId = Db::name('shop_product_attribute')->add($SaveData);
            if ($ReturnId) {
                // 参数值合计增加
                Db::name('shop_product_attrlist')->where('list_id', $post_data['list_id'])->setInc('attr_count');
                adminLog('新增商品参数：' . $SaveData['attr_name']);
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }

        $list_id = input('param.list_id/d', 0);
        $list    = $this->shop_product_attrlist_db->where('list_id', $list_id)->find();
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 编辑商品参数
     */
    public function attribute_edit()
    {
        //防止php超时
        function_exists('set_time_limit') && set_time_limit(0);

        if (IS_AJAX_POST) {
            $attr_values              = str_replace('_', '', input('attr_values')); // 替换特殊字符
            $attr_values              = str_replace('@', '', $attr_values); // 替换特殊字符
            $attr_values              = trim($attr_values);
            $post_data                = input('post.');
            $post_data['list_id'] = intval($post_data['list_id']);
            $post_data['attr_values'] = $attr_values;

            $SaveData = array(
                'attr_name'       => $post_data['attr_name'],
                'list_id'         => $post_data['list_id'],
                'attr_input_type' => isset($post_data['attr_input_type']) ? $post_data['attr_input_type'] : '',
                'attr_values'     => isset($post_data['attr_values']) ? $post_data['attr_values'] : '',
                'sort_order'      => $post_data['sort_order'],
                'update_time'     => getTime(),
            );

            $ReturnId = Db::name('shop_product_attribute')->where(['attr_id'=>$post_data['attr_id']])->update($SaveData);
            if ($ReturnId) {
                adminLog('编辑商品参数：' . $SaveData['attr_name']);
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }

        $id = input('param.id/d');
        $info = Db::name('shop_product_attribute')->where(['attr_id'=>$id])->find();
        if (empty($info)) $this->error('数据不存在，请联系管理员！');
        $this->assign('field', $info);

        $list = $this->shop_product_attrlist_db->where('list_id', $info['list_id'])->find();
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 删除商品参数
     */
    public function attribute_del()
    {
        $id_arr = input('del_id/a');
        $id_arr = eyIntval($id_arr);
        if (!empty($id_arr)) {
            $r = Db::name('shop_product_attribute')->where(['attr_id' => ['IN', $id_arr]])->delete();
            if ($r) {
                $IDCount = count($id_arr);
                Db::name('shop_product_attrlist')->where('list_id', input('list_id/d'))->setDec('attr_count', $IDCount);
                adminLog('删除商品参数-id：' . implode(',', $id_arr));
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('参数有误');
        }
    }

    /**
     * 动态获取商品参数输入框 根据不同的数据返回不同的输入框类型
     */
    public function ajax_get_shop_attr_input($typeid = '', $aid = '', $list_id = '')
    {
        $typeid       = intval($typeid);
        $aid          = intval($aid);
        $list_id      = intval($list_id);
        $productLogic = new ProductLogic();
        $str          = $productLogic->getShopAttrInput($aid, $typeid, $list_id);
        if (empty($str)) {
            $str = '<div style="font-size: 12px;text-align: center;">提示：该参数还没有参数值，若有需要请点击【<a href="' . url('Product/attribute_index', array('list_id' => $list_id)) . '">商品参数</a>】进行更多操作。</div>';
        }
        if (IS_AJAX) {
            exit($str);
        } else {
            return $str;
        }
    }

    /**
     * 动态获取商品参数输入框 根据不同的数据返回不同的输入框类型
     */
    public function ajax_get_attr_input($typeid = '', $aid = '', $list_id = '')
    {
        $typeid       = intval($typeid);
        $aid          = intval($aid);
        $list_id      = intval($list_id);
        $productLogic = new ProductLogic();
        $str          = $productLogic->getAttrInput($aid, $typeid, $list_id);
        if (empty($str)) {
            $str = '<div style="font-size: 12px;text-align: center;">提示：该参数还没有参数值，若有需要请点击【<a href="' . url('Product/attribute_index', array('list_id' => $list_id)) . '">商品参数</a>】进行更多操作。</div>';
        }
        if (IS_AJAX) {
            exit($str);
        } else {
            return $str;
        }
    }

    /**
     * 发布商品
     */
    public function release()
    {
        $typeid = input('param.typeid/d', 0);
        if (0 < $typeid) {
            $param = input('param.');
            $row   = Db::name('arctype')->field('current_channel')->find($typeid);
            /*针对不支持发布文档的模型*/
            if ($row['current_channel'] != 2) {
                $this->error('该栏目不支持发布商品！', url('ShopProduct/release'));
            }
            /*-----end*/

            $data    = [
                'typeid' => $typeid,
            ];
            $jumpUrl = url("ShopProduct/add", $data, true, true);
            header('Location: ' . $jumpUrl);
            exit;
        }

        /*允许发布文档列表的栏目*/
        $select_html = allow_release_arctype(0, [2]);
        $this->assign('select_html', $select_html);
        /*--end*/

        return $this->fetch();
    }

    //帮助
    public function help()
    {
        $system_originlist = tpSetting('system.system_originlist');
        $system_originlist = json_decode($system_originlist, true);
        $system_originlist = !empty($system_originlist) ? $system_originlist : [];
        $assign_data['system_originlist_str'] = implode(PHP_EOL, $system_originlist);
        $this->assign($assign_data);
    
        return $this->fetch();
    }
}